ret = SQLExecDirect( stmt2, "BEGIN", SQL_NTS );
ret = SQLTables( stmt1 );
for( SQLFetch( stmt1 ); ; SQLFetch( stmt1 ) )
{
// get catalog
// get schema
// get table
ret = SQLExecDirect( stmt2, "IF NOT EXIST( SELECT * FROM <my_table> WHERE name = <table_name> AND schema = <schema_name> ) INSERT INTO <my_table> VALUES( <table>, <schema>, .... )", SQL_NTS );
}
ret = SQLExecDirect( stmt2, "COMMIT", SQL_NTS );
(error checking and preparation omitted for clarity)
I have couple of questions in regards to the code above.
For the period of this transaction I need <my_table> to be locked so no read/write can be done. How can I do it?
I think I need one transaction. Am I right to make it on the INSERT statement query?
Should I make 2 queries in the transaction, splitting record verification and actual insertion?
This what I'm trying to accomplish (based on the answer from @DaleK):
INSERT INTO my_table SELECT( 0, 'abcatcol', (SELECT object_id FROM
sys.objects o, sys.schemas s WHERE s.schema_id = o.schema_id AND o.name =
'abcatcol' AND s.name = 'dbo')
WHERE NOT EXISTS(SELECT * FROM my_table WHERE abt_tnam='abcatcol' AND abt_ownr='dbo') );
However, I'm getting the following errors:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near ','.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'WHERE'.
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ')'.
I think only the first one is relevant - it is SELECT( 0,
.
Any idea what to do here?
As I mentioned in a comment on a previous question you asked, put the NOT EXISTS
condition in the WHERE
clause, and if you want exclusive access to the table (seems odd, but its what you asked for), use the TABLOCKX
(Exclusive Table Lock) hint e.g.
BEGIN TRAN;
INSERT INTO my_table (name, scheme, ...)
SELECT @table_name, @schema_name, ....
WHERE NOT EXISTS (
SELECT *
FROM my_table WITH (TABLOCKX)
WHERE name = @table_name
AND schema = @schema_name
);
COMMIT;
Note: I've written it all as T-SQL using variables - you can translate back into code.