Syntax Issue

  • I cant seem to get the syntax right, anyone help? it returns the error :-

    Line 5 Incorrect syntax near ''.

    declare @sql nvarchar(1000)

    select @sql = '' create table ey_report_temp (

    county nvarchar(100),

    fname nvarchar(100), '' + STUFF((SELECT distinct '','' + sport + '' nvarchar(10) ''

    FROM ey_report

    FOR XML PATH('''')

    ), 1, 1, '''') + '')''

    SQL2000

  • I don't understand what you are trying to do?

    If you want to create a table, you create column with data type and length.

    When you do the SELECT distinct statement , it may come back with multiple rows, so you want to create a table with multiple columns? Besides I don't think you can put a select statement in a create table statement.

  • I think this is what you are looking for:

    [font="Courier New"]DECLARE @sql NVARCHAR(1000)

    SELECT @sql = 'create table ey_report_temp (county nvarchar(100),fname nvarchar(100),  ' +

               STUFF((SELECT DISTINCT ',[' + sport + '] nvarchar(10) ' FROM ey_report FOR XML PATH('')), 1, 1, '') + ')'

    SELECT @sql

    [/font]

    I added the "[" & "]" because if you have spaces or reserved words in the sport column you need to qualify them.

  • Do you want to get rid of the space so you use the STUFF command?

    Why don't you use LTRIM?

  • It's the leading comma from the XML concatenation that they're trying to get rid of.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply