SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 execute Alter table from dynamic query in sp
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

shanmugaraj
Posting Yak Master

219 Posts

Posted - 04/18/2013 :  04:45:07  Show Profile  Send shanmugaraj a Yahoo! Message  Reply with Quote
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

India
52317 Posts

Posted - 04/18/2013 :  04:53:49  Show Profile  Reply with Quote
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 - 04/18/2013 :  05:02:47  Show Profile  Send shanmugaraj a Yahoo! Message  Reply with Quote
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

India
52317 Posts

Posted - 04/18/2013 :  05:09:36  Show Profile  Reply with Quote
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 - 04/18/2013 :  05:23:56  Show Profile  Send shanmugaraj a Yahoo! Message  Reply with Quote
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

India
52317 Posts

Posted - 04/18/2013 :  05:32:23  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000