Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

creating How do i create Temp table with declarations<!-- 864 --> Expand / Collapse
Author
Message
Posted Wednesday, August 20, 2014 1:28 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, August 21, 2014 7:50 AM
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
Post #1605583
Posted Wednesday, August 20, 2014 1:32 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:54 PM
Points: 13,471, Visits: 12,329
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)
Post #1605585
Posted Wednesday, August 20, 2014 2:30 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, August 21, 2014 7:50 AM
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.
Post #1605606
Posted Wednesday, August 20, 2014 2:51 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:54 PM
Points: 13,471, Visits: 12,329
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)
Post #1605611
Posted Wednesday, August 20, 2014 2:55 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 5:59 AM
Points: 3,660, Visits: 7,986
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1605613
Posted Thursday, August 21, 2014 7:24 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, August 21, 2014 7:50 AM
Points: 3, Visits: 5
Thanks got it to work using the example form the Link you posted
Post #1605802
Posted Thursday, August 21, 2014 7:44 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:54 PM
Points: 13,471, Visits: 12,329
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)
Post #1605822
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse