473,513 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I loop a #Temp Table and String values together in a variable?

103 New Member
I am writing a stored procedure and have a temp table that is dynmaic in size when it executes. I want to loop through each row in the temp and take a value from a column and then append that value in a varaible that would hold each value from all the rows. I am having trouble doing this. I can see the values, however, I get null for my return. Here is my code and Thanks in advance:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE #TEMP
  2. (
  3. ID INT Identity,
  4. ShortDescription INT,
  5. )
  6. INSERT INTO #TEMP 
  7. EXEC spRPProjectTemp 99
  8. DECLARE @var As VarChar(MAX) 
  9. DECLARE @Counter As INT
  10. SET @Counter = 1
  11. DECLARE @Index AS INT
  12. SET @Index = (SELECT MAX(ID) FROM #TEMP)
  13. DECLARE @varContainer AS Varchar(30)
  14.  
  15. WHILE @Counter < @Index + 1
  16. BEGIN
  17.       SET   @varContainer = (SELECT ShortDescription FROM #TEMP WHERE ID = @Counter)
  18. IF @var = ''
  19. BEGIN
  20.       SET @var = @varContainer
  21. SET @Counter = @Counter + 1
  22. END
  23. ELSE
  24. BEGIN
  25.       SET @var = @var+', '+@varContainer
  26.       SET @Counter = @Counter + 1
  27. END
  28. END
  29.  
  30. SELECT @var AS 'String'
  31. SELECT * FROM #TEMP 
Dec 26 '12 #1
3 7862
Rabbit
12,516 Recognized Expert Moderator MVP
You should get an error. I don't know why you're not. The reason is that ShortDescription is a integer and you have not cast it to varchar for your string. That is the underlying problem.

However,in SQL, you should avoid loops whenever possible. And if you have to loop on a recordset, use a cursor.

In this situation, if you are on SQL 2005 and above, you can use the XML capabilities to concatenate your values without having to use a loop of any kind.

Expand|Select|Wrap|Line Numbers
  1. SELECT ', ' + CAST(ShortDescription AS VARCHAR(30)
  2. FROM #temp
  3. FOR XML PATH('')
You can get rid of the first comma and space with the STUFF function if you wish.
Dec 27 '12 #2
ck9663
2,878 Recognized Expert Specialist
Here are some ideas on how to concatenate rows into a delimited string.

Happy Coding!!!


~~ CK
Dec 27 '12 #3
Brian Connelly
103 New Member
Thank you for the help.
Jan 22 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
16054
by: ImraneA | last post by:
Hi there Application : Access v2K/SQL 2K Jest : Using sproc to append records into SQL table Jest sproc : 1.Can have more than 1 record - so using ';' to separate each line from each other.
1
3258
by: Sergio | last post by:
Hi all! I have a problem with a temp table. I start creating my table: bdsqlado.execute ("CREATE TABLE #MyTable ...") There is no error. The sql string has been tested and when it's executed...
5
15546
by: Billy Cormic | last post by:
Hello, I am interested in dynamically creating temp tables using a variable in MS SQL Server 2000. For example: DECLARE @l_personsUID int select @l_personsUID = 9842
1
2233
by: Jim | last post by:
For some reason the compiler is telling me that I must declarethe variable @costcenter_tmp on lines 74 and 98...but if i put a select statement in ther (for testing) before the loop I get data back...
4
6030
by: Andre Arpin | last post by:
I am new at sql so would appreciate some help I have the name of a table in alocal variable is it possible to select this table DECLARE @name sysname SET @name = 'tblSniffedItems' PRINT...
11
13591
by: laurenq uantrell | last post by:
I want to take the contents from a table of appointments and insert the appointments for any given month into a temp table where all the appointments for each day are inserted into a single row...
1
2880
by: Robert McEuen | last post by:
Using Access 97 on WinXP I have data in a DB2 table that I'm trying to get into an identical table in my backend db. Based on volume of data and frequency of download, I'm trying to avoid...
9
2272
by: BillCo | last post by:
I have a function which clears out a temporary table and then fills it with values from a recordset. One of the table fields is indexed with no duplicates. Very rarely - almost enough to ignore it...
2
5522
by: Burbletrack | last post by:
Hi All, Hope someone can help me... Im trying to highlight the advantages of using table variables as apposed to temp tables within single scope. My manager seems to believe that table...
3
2433
by: Lennart | last post by:
Any thoughts on the following scenario anyone? During a performance test I discovered that the application asked one specific query extremely often. It turned out that this particular query...
0
7264
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7166
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7543
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7106
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7534
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.