C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » SQL Server »

alter table in cursor


Posted Date: 23 Nov 2008      Posted By: Gaurav Agrawal      Member Level: Diamond     Points: 1   Responses: 1



Hi friends i am using a cursor for alter a table i.e.

declare @pptitle as varchar(20)
create table temp(Name varchar(50)) -- drop table temp
declare cur cursor for select distinct(title) from tbl_pointMst
open cur
fetch next from cur into @pptitle
WHILE @@FETCH_STATUS = 0
BEGIN
print @pptitle
alter table temp add @pptitle varchar(50)
fetch next from cur into @pptitle
end
close cur
deallocate cur

but it gives an error and i.e.

Msg 102, Level 15, State 1, Line 11
Incorrect syntax near '@pptitle'.


How i can done this plz help me.............

GA

Thanks & Regards,

Gaurav Agrawal
Sr.Software Engineer
gaur1982@yahoo.com
09829373514





Responses

Author: Shankar    23 Nov 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 5

Hi Gaurav,

Pls use the below code

declare @pptitle as varchar(20)
create table temp(Name varchar(50)) -- drop table temp
declare cur cursor for select distinct(CategoryName) from dbo.Categories
open cur
fetch next from cur into @pptitle
WHILE @@FETCH_STATUS = 0
BEGIN
print @pptitle
exec('alter table temp add ' + @pptitle + ' varchar(50)')
fetch next from cur into @pptitle
end
close cur
deallocate cur

Thanks
A S

If u need any clarifications pls mail me.

Thanks and Regards,
A S.




Note - Don't forget to rate this response [Poor / Fine / Good / Great / Excellent]



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : COMPARISON BETWEEN SYSTEM DATE AND DATE STORED IN DATABASE
Previous : How to copy the stored procedures from one database to another database through query?
Return to Discussion Forum
Post New Message
Category: SQL Server

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use