sql server - sp_execute for multiple dynamic T-SQL statements ak a batch -


ideally i'd execute several sql statements single exec sp_executesql statement. example use 1 if exists determine if second statement run:

drop proc ai_importdataaddlistposn go  create proc ai_importdataaddlistposn(@paramtablename nvarchar(255), @debug int )  begin declare @sql nvarchar(4000) set @sql = n'if not exists(select * syscolumns inner join sysobjects on syscolumns.id = sysobjects.id sysobjects.name = ''' + @paramtablename + ''' , syscolumns.name = ''listposn'');'             + 'alter table [' + @paramtablename + '] add listposn int identity(1,1)' if @debug = 1 print @sql exec sp_executesql @sql end go  exec ai_importdataaddlistposn deptformove, 1 

i realise example not test existence of table first, simplified example not real problem. aware of sql injection , how combat it. i'm reasonably happy both statements sql

i thought ";" may act statement terminator

i aware of sql injection , how combat it. i'm reasonably happy both statements sql

there's scant evidence of in question. should using parameterisation , quotename.

i thought ";" may act statement terminator

it don't want statement terminator there.

if 1=1; select 'foo'; 

is invalid syntax.

if 1=1 select 'foo'; 

would work fine however. need replace semicolon after boolean_expression white space.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -