Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


creating How do i create Temp table with declarations<!-- 864 -->


creating How do i create Temp table with declarations<!-- 864 -->

Author
Message
suriaho
suriaho
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 5
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
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16680 Visits: 17038
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
suriaho
suriaho
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 5
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.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16680 Visits: 17038
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8592 Visits: 18175
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)




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
suriaho
suriaho
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 5
Thanks got it to work using the example form the Link you posted
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16680 Visits: 17038
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search