I'm using Azure SQL Database (T-SQL), and having problems with a "CREATE TABLE IF NOT EXISTS" query. I've already read the links here, and probably a few others:
My code:
declare @tableName nvarchar(max);
declare @sqlquery nvarchar(max);
SET @tableName = 'Russ';
SET @sqlquery = 'if not exists (select * from sysobjects where name=' + @tableName + 'and xtype=''U'') create table ' + @tableName + '([ID] [bigint] PRIMARY KEY NOT NULL DEFAULT (NEXT VALUE FOR dbo.standardsequence), [Name One] [nvarchar](64) NOT NULL, [Name Two] [nvarchar](64) NOT NULL)';
exec sp_executesql @sqlquery;
- Error I'm getting is "Syntax error near xtype='U'", but I've escaped that part so I don't understand the problem there.
- Every place says to use sp_executesql, which I'm doing. I also tried setting the variable in the sp_executesql statement (ex: EXECUTE sp_executesql @SQL, N'@tableName VARCHAR(10)', 'Russ'), but it doesn't work that way either.
- Query works perfectly fine without variables, so I'm pretty sure the problem is with that, but just don't know why.
What am I doing wrong? I am stumped. If there was a Stumped Bureau, they'd declare me officially stumped.