|
-
Apr 12th, 2008, 02:21 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2005] Returning SQLCOMMAND results to a string
I have a function that will be returning my list of customers it gets from a SQL table. But instead of just returning the results of the query in its native sql format, I need it to return them as a string, with each result after the other with a comma in the middle, like this: Customer1,Customer 2, Customer 3, etc...
I know I can use the StringBuilder to build the string, but how can I tell it that I need the results in a string?
Thanks!

In the unlikely event that I answer your question correctly, please Rate my post
Using Visual Studio 2005 Professional 
-
Apr 12th, 2008, 02:36 PM
#2
Re: [2005] Returning SQLCOMMAND results to a string
-
Apr 12th, 2008, 02:38 PM
#3
Re: [2005] Returning SQLCOMMAND results to a string
You won't need the string builder for this. You can make a stored procedure in your sql database with an nvarchar(max) output parameter. Then, build the delimited string within your stored procedure.
-
Apr 12th, 2008, 04:32 PM
#4
Re: [2005] Returning SQLCOMMAND results to a string
Like this
Code:
Set NoCount On
Declare @TestTbl Table (CustName varchar(10))
Insert into @TestTbl values ('Adams')
Insert into @TestTbl values ('Jones')
Insert into @TestTbl values ('Smith')
-- Above is just setting up some test table data
Declare @RtnString varchar(1000)
Select @RtnString=IsNull(@RtnString+', ','')+CustName
From @TestTbl
Order by CustName -- Not really a SELECT - just building a string up
Select @RtnString "Customers" -- This select returns the data out of the SPROC
And it looks like this
Code:
Customers
-------------------
Adams, Jones, Smith
-
Apr 14th, 2008, 02:24 PM
#5
Thread Starter
Fanatic Member
Re: [2005] Returning SQLCOMMAND results to a string
Thats very helpful, but I'm not to familiar with stored procs, where would I put my query that selects the column into the TempTbl?
Here's the query:
Code:
Select Contact FROM Users WHERE Status=1 and Contact <>''

In the unlikely event that I answer your question correctly, please Rate my post
Using Visual Studio 2005 Professional 
-
Apr 14th, 2008, 02:46 PM
#6
Thread Starter
Fanatic Member
Re: [2005] Returning SQLCOMMAND results to a string
Ahh nevermind, I worked with it and got it working.
Here's what I did:
Code:
CREATE PROCEDURE
dbo.ReturnEmployeesAsString
AS
Set NoCount On
CREATE TABLE #temp (EmpName varchar(40))
Insert into #temp SELECT Contact FROM Users WHERE Status=1 and Contact <>''
Declare @RtnString varchar(1500)
Select @RtnString=IsNull(@RtnString+', ','')+EmpName
From #temp
Order by EmpName
Select @RtnString "Employees"
Drop table #temp
GO

In the unlikely event that I answer your question correctly, please Rate my post
Using Visual Studio 2005 Professional 
-
Apr 14th, 2008, 02:52 PM
#7
Thread Starter
Fanatic Member
Re: [2005] Returning SQLCOMMAND results to a string
Hmm my datatype isn't long enough. Is there a datatype that I don't have to configure a maximum length?

In the unlikely event that I answer your question correctly, please Rate my post
Using Visual Studio 2005 Professional 
-
Apr 14th, 2008, 02:52 PM
#8
Re: [2005] Returning SQLCOMMAND results to a string
My temp table was for "test" purposes.
You can get rid of that and simply select directly from your table
Code:
CREATE PROCEDURE
dbo.ReturnEmployeesAsString
AS
Set NoCount On
Declare @RtnString varchar(4000)
SELECT @RtnString=IsNull(@RtnString+', ','')+Contact
FROM Users WHERE Status=1 and Contact <>''
Order by Contact
Select @RtnString "Employees"
GO
I just saw your second post - make it 4000. Or even 8000.
Keep in mind that when you test it in Mgt Studio it won't show the whole column
-
Apr 14th, 2008, 02:56 PM
#9
Thread Starter
Fanatic Member
Re: [2005] Returning SQLCOMMAND results to a string
 Originally Posted by szlamany
My temp table was for "test" purposes.
You can get rid of that and simply select directly from your table
Code:
CREATE PROCEDURE
dbo.ReturnEmployeesAsString
AS
Set NoCount On
Declare @RtnString varchar(4000)
SELECT @RtnString=IsNull(@RtnString+', ','')+Contact
FROM Users WHERE Status=1 and Contact <>''
Order by Contact
Select @RtnString "Employees"
GO
I just saw your second post - make it 4000. Or even 8000.
Keep in mind that when you test it in Mgt Studio it won't show the whole column
Ahh that makes more sense And you're right, I'm looking at the results in Query Analyzer, probably why I'm not seeing it all.

In the unlikely event that I answer your question correctly, please Rate my post
Using Visual Studio 2005 Professional 
-
Apr 14th, 2008, 03:06 PM
#10
Re: [2005] Returning SQLCOMMAND results to a string
There is a way to increase the width that QA displays - it's under options somewhere.
Stored procedures are so nice for this type of operation - remove this logic from the UI and put it in the database where it's easy to access and modify in the future.
-
Apr 14th, 2008, 04:21 PM
#11
Thread Starter
Fanatic Member
Re: [2005] Returning SQLCOMMAND results to a string
Well it works great, in Query Analyzer. When I try to use VB.net to run the proc, it only returns '-1' even with the .ToString at the end.

In the unlikely event that I answer your question correctly, please Rate my post
Using Visual Studio 2005 Professional 
-
Apr 14th, 2008, 04:24 PM
#12
Re: [2005] Returning SQLCOMMAND results to a string
-
Apr 14th, 2008, 05:09 PM
#13
Thread Starter
Fanatic Member
Re: [RESOLVED] [2005] Returning SQLCOMMAND results to a string
Sorry I figured it out. I was trying to use command.ExecuteNonQuery instead of command.ExecuteScalar.
I figured it out, but thanks anyway! You've been a great help!

In the unlikely event that I answer your question correctly, please Rate my post
Using Visual Studio 2005 Professional 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|