Results 1 to 13 of 13

Thread: [RESOLVED] [2005] Returning SQLCOMMAND results to a string

  1. #1

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Resolved [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

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] Returning SQLCOMMAND results to a string


  3. #3
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    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.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    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

  6. #6

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    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

  7. #7

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    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

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: [2005] Returning SQLCOMMAND results to a string

    Quote 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

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  11. #11

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    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

  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [2005] Returning SQLCOMMAND results to a string

    Show some code please

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  13. #13

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    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
  •  



Click Here to Expand Forum to Full Width