views:

144

answers:

3

The line cmd.ExecuteNonQuery();

cmd.CommandText

CREATE TRIGGER  subscription_trig_0  ON  subscription   AFTER INSERT  AS  UPDATE user_data SET msg_count=msg_count+1 FROM user_data
JOIN INSERTED ON user_data.id = INSERTED.recipient; 

The exception:

Incorrect syntax near the keyword 'TRIGGER'.

Then using VS 2010, connected to the very same file (a mdf file) I run the query above and I get a success message. WTF!

+2  A: 

Options

  • The 1st line of the actual SQL sent is not CREATE TRIGGER
  • CommandType is wrong (eg it's trying to add EXEC or some "prepare" commands)

Use SQL profiler to see exactly what you're sending on to the DB engine (you actually have Express edition that is hosting the MDF)

gbn
I'm confused. What do you mean by "The 1st line of the actual SQL sent is not CREATE TRIGGER"? CommandType is set to TEXT. How do i use/where is SQL profiler ? So far i had 0 luck and >5 threads about problems with MS, SQL and IDEs. But anyways, i still dont understand the solution and may not understand while looking at the profiler.
acidzombie24
+1  A: 

Do you have CommandType set wrong?

Gabe
it set to text. Should it not be for triggers?
acidzombie24
`Text` is correct for what you are doing. Please post all the C# code that creates this trigger.
Gabe
Its pretty ugly since there are regex.replace. and as i said the syntax works in visual studios. Maybe i'll try using a fresh sqlcommand object.
acidzombie24
I havent completely tested it but it appears that does solve the problem. I cant imagine .parameters are affecting it when there are no @0 in the cmd text. OMG i just check it. It does. Theres nothing that refers to the param in the query and it explodes with a completely irrelevant error msg WTF.
acidzombie24
The moment you add a @parameter to a command, its invocation changes from "T-SQL Batch" type to "RPC", which are different types on the TDS protocol (see http://www.freetds.org/tds.html#requests). You don't specify what `cmd` is, each client type treats the distinction differently (ODBC, OleDB, SqlClient). Most notably, the OleDB client adds an EXEC in front of the requests and the result is incorrect syntax usually. As obscure subject as this may be, a developer worth its salt knows it, if not from spec then from experience.
Remus Rusanu
@Remus: If your here to see this, do you have any idea why the last .ExecuteQuery didnt delete/clear it? I believe i used it in if exist(statement to check if triggername exist @0) drop trigger triggername (i couldnt do @0 IIRC). That was the weirdest part of it all. Always clearing it when i execute and it not for this query.
acidzombie24
@Acid: there is a lot of information you leave out and assume we know. What client (OleDB, SqlClient)? Did you reuse the command between executions? You better prepare a more clear explanation and post a separate question.
Remus Rusanu
A: 

I am not sure why this is failing but if I were you I would use SMO for ddl queries. In this case you need Trigger.Create Method.

Giorgi