Home Forums SQL Server 2005 Administering creating How do i create Temp table with declarations<!-- 864 --> RE: creating How do i create Temp table with declarations<!-- 864 -->

  • 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