Reputation: 123
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!
Upvotes: 1
Views: 1322
Reputation: 62159
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.
Upvotes: 5