简体   繁体   中英

I have a problem with my SQL statement

    Threads 
------- 
ThreadID
 UsersID 
Date 
ThreadTitle
 ThreadParagraph 
ThreadClosed 

  Topics 
-----
 TopicsID 
Theme
 Topics 
Date 

Here is my statement:

  StringBuilder insertCommand = new StringBuilder();
    insertCommand.Append("DECLARE @TopicsID int");
    insertCommand.Append("INSERT INTO Topics(Theme,Topics,Date)");
    insertCommand.Append("VALUES(@topic,@subTopic,GETDATE())");
    insertCommand.Append("SET @TopicsID = SCOPE_IDENTITY()");

    insertCommand.Append("INSERT INTO Threads(UsersID,TopicsID,Date,ThreadTitle,ThreadParagraph,ThreadClosed)");
    insertCommand.Append("VALUES(@uniqueIdentifier,@TopicsID,GETDATE(),@questionTitle,@questionParagraph,0)");

I get this:

Incorrect syntax near the keyword 'INTO'. Must declare the scalar variable "@TopicsID". Must declare the scalar variable "@TopicsID".

The first thing I notice is:

insertCommand.Append("VALUES('@topic,@subTopic,GETDATE()')");

might need to be:

insertCommand.Append("VALUES(@topic,@subTopic,GETDATE())");

It looks like you have some extra single quotes in there.

You need a semi-colon after DECLARE @TopicsID int

That will take care of the "incorrect syntax" and declaring the scalar variable.

And I believe that you need to remove the single quotes around your three VALUES . That is causing it to think you have supplied only one VALUE instead of three.

Try insertCommand.AppendLine

SQL sees

DECLARE @TopicsID intINSERT INTO Topics(Theme,Topics,Date)...

You need to separate the distinct statements

Your single quotes on the last line are wrong - its turning it all onto one string:

Change

  insertCommand.Append("VALUES('@uniqueIdentifier,@TopicsID,GETDATE(),@questionTitle,@questionParagraph,0')");

to

insertCommand.Append("VALUES(@uniqueIdentifier,@TopicsID,GETDATE(),@questionTitle,@questionParagraph,0)");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM