Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 execute Alter table from dynamic query in sp

Author  Topic 

shanmugaraj
Posting Yak Master

219 Posts

Posted - 2013-04-18 : 04:45:07
Hi have an sp to create alter table statement.
once the alter table statement is created, i need to execute the statement.



create procedure sp_get_missing_column
declare @str_alter_query varchar(max)

BEGIN
set @str_alter_query = dept_id,location_id

set @str_alter_query = 'Alter table temp_dynamic add '+replace(@str_alter_query, ',', ' nvarchar(max) , ') +' nvarchar(max)'

EXEC @str_alter_query -- NOT WORKING


END


THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-18 : 04:53:49
it should be

create procedure sp_get_missing_column
declare @str_alter_query varchar(max)

BEGIN
set @str_alter_query = 'dept_id,location_id'

set @str_alter_query = 'Alter table temp_dynamic add '+replace(@str_alter_query, ',', ' nvarchar(max) , ') +' nvarchar(max)'

EXEC @str_alter_query -- NOT WORKING


END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

shanmugaraj
Posting Yak Master

219 Posts

Posted - 2013-04-18 : 05:02:47
The query is not executing and geting error

exec sp_get_missing_column 'dept_id'
error :

Could not find stored procedure 'Alter table temp_dynamic add dept_id nvarchar(max)'.


THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-18 : 05:09:36
try with () around variable




create procedure sp_get_missing_column
declare @str_alter_query varchar(max)

BEGIN
set @str_alter_query = 'dept_id,location_id'

set @str_alter_query = 'Alter table temp_dynamic add '+replace(@str_alter_query, ',', ' nvarchar(max) , ') +' nvarchar(max)'

EXEC (@str_alter_query) -- NOT WORKING


END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

shanmugaraj
Posting Yak Master

219 Posts

Posted - 2013-04-18 : 05:23:56
SELECT @str_alter_query = 'Alter table temp_dynamic add '+replace(@str_alter_query, ',', ' nvarchar(max) , ') +' nvarchar(max)'

THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-18 : 05:32:23
did you try my suggestion? did it work?


create procedure sp_get_missing_column
declare @str_alter_query varchar(max)

BEGIN
set @str_alter_query = 'dept_id,location_id'

set @str_alter_query = 'Alter table temp_dynamic add '+replace(@str_alter_query, ',', ' nvarchar(max) , ') +' nvarchar(max)'

EXEC (@str_alter_query) -- NOT WORKING


END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -