August 20, 2014 at 1:28 pm
I finally got it to work
so basically what i was trying to do example is to change one column to a row
example:
Subject variable
101 Dob
102 COB
108 EDD
So i wanted to change the above to because each variable will hold a value for each subject
Subject dob cob Edd
101
102
108
August 20, 2014 at 1:32 pm
Hi and welcome to the forums. I don't understand what you mean about putting your variables in a temp table. I don't see a temp table declared, nor is there a reason for it. What I truly don't understand is the cursor. What is the reason for the cursor and what are you trying to do inside the cursor. For the records cursors are horrible for performance. They have their place for sure but modifying data (which is what it looks like you are doing) is not the right time for such a construct. We can help you fix your code and get rid of the cursor if you can help us understand what you are trying to do.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
August 20, 2014 at 2:30 pm
Thanks For the reply. Basically i need to put the result of my first post into a temp table so i can use for my calculations.
August 20, 2014 at 2:51 pm
suriaho (8/20/2014)
Thanks For the reply. Basically i need to put the result of my first post into a temp table so i can use for my calculations.
This is not helping me understand what you are trying to do. What is your first post? Why does it need to be in a temp table? Aren't the values already in a variable? Why do you need a cursor? You do know that you can do calculations from variables right? It doesn't have to be in a table.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
August 20, 2014 at 2:55 pm
As Sean said, there are better ways to do it than a cursor. Here's an option using XML PATH which is explained in the following article: http://www.sqlservercentral.com/articles/comma+separated+list/71700/
To solve your problem, have you considered using a global temp table (##Table)?
DECLARE @firstsql VARCHAR(max)
SET @firstsql = 'SELECT Site, [Family ID], [First Name], [Last Name], [Survey Status], [Date Started], [Date Completed], '
+ (SELECT
', [' + cf.sourceSystemFieldID + '] AS ' + ISNULL(NULLIF( tf.preferredFieldName, 'NULL'), 'EDIT')
FROM [itmidw].[tblCrfTranslationField] tf
JOIN itmidw.tblCrfFields cf ON tf.fieldID = cf.fieldID
WHERE crfVersionName = '6MonthBaby: v1'
FOR XML PATH('')) --Change HERE
+ ' INTO ##AGlobalTempTable'
+ ' FROM etl.SurveyBaby6_Stage'
PRINT @firstsql
EXEC (@firstSQL)
August 21, 2014 at 7:24 am
Thanks got it to work using the example form the Link you posted
August 21, 2014 at 7:44 am
suriaho (8/21/2014)
Thanks got it to work using the example form the Link you posted
Glad that worked for you and thanks for letting us know.
+1000 to Luis for his ability to decipher what you wanted.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply