473,672 Members | 2,600 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 7881
Rabbit
12,516 Recognized Expert Moderator MVP
You should get an error. I don't know why you're not. The reason is that ShortDescriptio n 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
16065
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
3265
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 in the sql query analyzer it really creates the table. After creating the table, I execute an insert statement:
5
15590
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
2241
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 from the temp table.. why is this happening to this temp table and no others?..heres my code..its a little lengthy --Fiscal Year
4
6036
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 @name SELECT * FROM @name
11
13606
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 with a column for each day of the month. Is there a simple way to do this? I have a recordset that looks like: SELECT a.Date,
1
2893
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 having a temp table created in my frontend. I downloaded the temp database code from Tony Toews's site, and altered it to fit my needs (I thought), and it functions properly all the way to the point where the data is inserted into a new table (see
9
2287
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 - two users will call the function at once causing one of the functions to attempt to update the table with duplicate values. Is there a way to lock the table to prevent this happening? Function() 'wait until tmpTable is free
2
5536
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 variables are not advantageous because they reside in memory. He also seems to believe that temp tables do not use memory...
3
2449
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 where asked 25/50/100 or 200 times from a "htmlpage", dependent of user preferences. I figured that using a global temp table, looping and inserting, then join would do the trick. However, it turned out that this killed performance totally. Why,...
0
8406
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8932
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8831
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8609
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8683
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6240
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2821
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 we have to send another system
2
1819
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.