1

Is it possbible to return a string in SQL?

I have a stored procedure, and this creates a nice text string. Would I have to pass this into a temp table and somehow read it from C# or can I simply return a string

JustAnotherDeveloper
  • 3,167
  • 11
  • 37
  • 68

6 Answers6

5

just add that at the end of you stored procedure:

select myString
juergen d
  • 201,996
  • 37
  • 293
  • 362
2

If the stored procedure only returns a single row, just call ExecuteScalar, which returns the first row and column of the query you executed. Here's an MSDN article on the subject.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
2

I would suggest that you use a FUNCTION instead of a STORED PROCEDURE. You can return either tables or whatever you want from a function.

There are some limitations though. You can dig a little deeper into the differences to see what is used when. Here's a link that can help you out get started:

Function vs. Stored Procedure in SQL Server

If you want to use stored procedure anyway, you can either return a single row, single column result set, using SELECT, or just use an output parameter.

Community
  • 1
  • 1
TheBoyan
  • 6,802
  • 3
  • 45
  • 61
0

I would ask WHY are you returning a single string from SQL Server? Occasionally it might be appropriate, but SQL Server is designed to return sets of data. If you are looking at individual values, you might want to rethink your architecture.

Bring back the whole row. Decide to filter it to a single column on the business side (in .NET). Use the DataReader if you are concerned about optimizing.

Andrew
  • 326
  • 1
  • 7
0

select "ok"

or

return "ok"

you can try

yapingchen
  • 836
  • 5
  • 4
0

Fixed by using XML PATH and select as text.

JustAnotherDeveloper
  • 3,167
  • 11
  • 37
  • 68