开发者

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'.开发者_如何学Python 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)");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜