T-SQL Statement Error

  • Hello Experts,

    Iโ€™m trying to write a simple T-SQL statement but having error which Iโ€™m not able to understand at all. SQL is keep complying and saying

    ---------------------------------------------------------

    Msg 102, Level 15, State 1, Line 1

    Incorrect syntax near ','.

    ---------------------------------------------------------

    Here is my code

    -----------------------------------------------------

    declare @AD_TransactionErrorAuditID varchar(30),

    @AD_TransactionErrorAuditSeqNum varchar(30),

    @AD_ErrorCode varchar(10),

    @AD_ErrorMessage varchar(40),

    @AD_Fetch_Data varchar(250)

    select @AD_TransactionErrorAuditID = 'TransactionErrorAuditID'

    select @AD_TransactionErrorAuditSeqNum = 'TransactionErrorAuditSequentialNumber'

    select @AD_ErrorCode = 'ErrorCode'

    select @AD_ErrorMessage = 'ErrorMessage'

    select @AD_Fetch_Data = +@AD_TransactionErrorAuditID+ ', '

    +@AD_TransactionErrorAuditSeqNum+ ', '

    +@AD_ErrorCode+ ', '

    +@AD_ErrorMessage

    EXEC (@AD_Fetch_Data)

    GO

    ----------------------------------------------------

    Can anybody see what am I doing wrong?

    Thanks a lot in advance.

  • Change your exec command to a print command. You're telling it to execute something that isn't an SQL command.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • From the first look, I cant understand what you trying to do here?

    EXEC (@AD_Fetch_Data) ----- What do you want to execute?

    @AD_Fetch_Data is going to store this value "TransactionErrorAuditID, TransactionErrorAuditSequentia, ErrorCode, ErrorMessage"

    what will you execute it to?

    Updated:

    Well I started to reply and was caught in a conversation and could not reply in time, mean while

    GSquared has mentioned what you need to do


    Bru Medishetty

    Blog -- LearnSQLWithBru

    Join on Facebook Page Facebook.comLearnSQLWithBru

    Twitter -- BruMedishetty

  • Thanks for you input guys. Okay I'm trying to understand in's and out's and found a very interesting exmaple as follows. This can explain you even better If you don't understand What I'm trying to do.

    http://www.java2s.com/Tutorial/SQLServer/0400__Transact-SQL/SimpleSELECTquerywrittenusingvariablesforfieldnames.htm

  • Thanks SSCertifiable for you suggestion. Full points to you. It worked. Also I hope for those who don't understand what I'm doing they need to work on their clearance level.

  • So here is my code if still people donโ€™t understand what I am tying to do. The only catch is I changes @AD_TransactionErrorAuditSeqNum varchar(40) from varchar(30). This is another way to write SQL Statement. AND IT EXECUTE TOO.

    ---------------------------------------------------------

    Here is my code

    -----------------------------------------------------

    declare @AD_TransactionErrorAuditID varchar(30),

    @AD_TransactionErrorAuditSeqNum varchar(40),

    @AD_ErrorCode varchar(10),

    @AD_ErrorMessage varchar(40),

    @AD_Fetch_Data varchar(250)

    select @AD_TransactionErrorAuditID = 'TransactionErrorAuditID'

    select @AD_TransactionErrorAuditSeqNum = 'TransactionErrorAuditSequentialNumber'

    select @AD_ErrorCode = 'ErrorCode'

    select @AD_ErrorMessage = 'ErrorMessage'

    select @AD_Fetch_Data = +@AD_TransactionErrorAuditID+ ', '

    +@AD_TransactionErrorAuditSeqNum+ ', '

    +@AD_ErrorCode+ ', '

    +@AD_ErrorMessage

    EXEC (@AD_Fetch_Data)

    GO

    ----------------------------------------------------

  • Not sure what you mean by "clearance level", but glad I could help out.

    It's pretty standard, when working with dynamic SQL, to have a print command so you can review and test the resulting string, before you execute it. Adopt that as a standard practice, and it'll help.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Thanks again SSCertifiable. It will be my rule from now to use print. This is first time I'm using SQL Server that's why tying to understand it as much & as deeply as I can. Thanks again for your input

  • Thanks SSCertifiable for you suggestion. Full points to you. It worked. Also I hope for those who don't understand what I'm doing they need to work on their clearance level.

    Speaking about Clearance level, First you need to know that you have not posted what you wanted to achieve with your SQL Statement. Without which it is not understandable what you are trying to do.

    And also about Clearance level, I don't know what do you know but it is obvious that you are not clear about something such as this "Thanks SSCertifiable" It is GSquared who helped not SSCertifiable ๐Ÿ˜›


    Bru Medishetty

    Blog -- LearnSQLWithBru

    Join on Facebook Page Facebook.comLearnSQLWithBru

    Twitter -- BruMedishetty

  • Bru Medishetty (10/29/2009)


    Thanks SSCertifiable for you suggestion. Full points to you. It worked. Also I hope for those who don't understand what I'm doing they need to work on their clearance level.

    ....

    And also about Clearance level, I don't know what do you know but it is obvious that you are not clear about something such as this "Thanks SSCertifiable" It is GSquared who helped not SSCertifiable ๐Ÿ˜›

    Don't worry about it. I'm used to that.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • mr_adeelalisyed (10/29/2009)


    So here is my code if still people donโ€™t understand what I am tying to do. The only catch is I changes @AD_TransactionErrorAuditSeqNum varchar(40) from varchar(30). This is another way to write SQL Statement. AND IT EXECUTE TOO.

    ---------------------------------------------------------

    Here is my code

    -----------------------------------------------------

    declare @AD_TransactionErrorAuditID varchar(30),

    @AD_TransactionErrorAuditSeqNum varchar(40),

    @AD_ErrorCode varchar(10),

    @AD_ErrorMessage varchar(40),

    @AD_Fetch_Data varchar(250)

    select @AD_TransactionErrorAuditID = 'TransactionErrorAuditID'

    select @AD_TransactionErrorAuditSeqNum = 'TransactionErrorAuditSequentialNumber'

    select @AD_ErrorCode = 'ErrorCode'

    select @AD_ErrorMessage = 'ErrorMessage'

    select @AD_Fetch_Data = +@AD_TransactionErrorAuditID+ ', '

    +@AD_TransactionErrorAuditSeqNum+ ', '

    +@AD_ErrorCode+ ', '

    +@AD_ErrorMessage

    EXEC (@AD_Fetch_Data)

    GO

    ----------------------------------------------------

    Nope, still confused as the above code when cut and paste into SSMS fails. A print (@AD_Fetch_Data) simply returns this :

    TransactionErrorAuditID, TransactionErrorAuditSequentialNumber, ErrorCode, ErrorMessage

  • He's trying to build a select statement, Lynn. Just not communicating it as clearly as we'd like, but that happens.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • I sorta figured that, but what he has posted just doesn't execute. ๐Ÿ˜‰

  • Hi mr_adeelalisyed,

    The reference posted by you in

    http://www.java2s.com/Tutorial/SQLServer/0400__Transact-SQL/SimpleSELECTquerywrittenusingvariablesforfieldnames.htm

    gives a slight hint of issue that you might be facing.

    I think you are trying to execute

    TransactionErrorAuditID, TransactionErrorAuditSequentia, ErrorCode, ErrorMessage

    But it is not a valid SQL Statement.

    According to the link, you should add ' SELECT ' at the start of the @AD_Fetch_Data variable and ' From ValidTableName ' at the end of @AD_Fetch_Data variable (as done in the example + ' FROM authors'

    ). By adding these two, the statement will become valid executable statement and you will receive no error.

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sometimes, winning is not an issue but trying.
    You can check my BLOG
    [font="Arial Black"]here[/font][/url][/right]

Viewing 14 posts - 1 through 13 (of 13 total)

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