简体   繁体   English

我的 SQL 语句有问题

[英]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'.关键字“INTO”附近的语法不正确。 Must declare the scalar variable "@TopicsID".必须声明标量变量“@TopicsID”。 Must declare the scalar variable "@TopicsID".必须声明标量变量“@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 intDECLARE @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 .而且我相信您需要删除三个VALUES周围的单引号 That is causing it to think you have supplied only one VALUE instead of three.这导致它认为您只提供了一个VALUE而不是三个。

Try insertCommand.AppendLine尝试insertCommand.AppendLine

SQL sees SQL 看到

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)");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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