1

Why does this code throw an error?

using (ProviderContext db = new ProviderContext())
{
  string sqlcommand = @"GO CREATE TRIGGER [Trigger] ON [dbo].[News] FOR    insert AS BEGIN SET NOCOUNT OFF insert [Statistics](NewsStatID) select NewsID from inserted update [Statistics] set Hits = '0' update [Statistics] set Positive = '0' update [Statistics] set Negative = '0' END";
  db.Database.ExecuteSqlCommand(sqlcommand);
  db.SaveChanges();
}

An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: Incorrect syntax near the keyword 'TRIGGER'.

Thank you in advance!

1 Answer 1

5

Because your syntax is wrong?

GO is not a keyword SQL Server understands - it is a feature of the management studio.

To quote from the documentation (obvious that is in there, is it not?):

Signals the end of a batch of Transact-SQL statements to the SQL Server utilities.

Now, you are not in the utilities, so the key word is not recognized. It also makes no sense. As in: it is not needed.

And btw., that is also wrong:

set Negative = '0'

Youc an just write

set Negative = 0

SQL Server is totally capable of dealing with numbers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.