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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 alter table and insert the data

Author  Topic 

desikankannan
Posting Yak Master

152 Posts

Posted - 2013-11-12 : 12:45:17
I try to alter the table and insert below code i have drop 5 columns and alter again the table. please help me to solve the issue

IF NOT EXISTS ( SELECT * FROM syscolumns WHERE id = OBJECT_ID('DeathData') AND name IN('DonationTitle','SendDonationsTo','DonationAddress','DonationCity','DonationState','DonationZip' ))
BEGIN
ALTER TABLE DeathData
ADD DonationTitle varchar(50) ,SendDonationsTo varchar(50),DonationAddress varchar(30),DonationCity varchar(30),DonationState varchar(30), DonationZip varchar(30)
END
IF EXISTS ( SELECT * FROM syscolumns WHERE id = OBJECT_ID('DeathData') AND name IN('DonationTitle','SendDonationsTo','DonationAddress','DonationCity','DonationState','DonationZip' ))
BEGIN
insert into tbldonations (donationstitle , donationsto,donaddress,doncity,donstate,donzip)
select distinct DonationTitle , SendDonationsTo,DonationAddress,DonationCity,DonationState,DonationZip from DeathData where SendDonationsTo != ''
END
GO

error

Msg 207, Level 16, State 1, Line 9
Invalid column name 'SendDonationsTo'.
Msg 207, Level 16, State 1, Line 9
Invalid column name 'DonationTitle'.
Msg 207, Level 16, State 1, Line 9
Invalid column name 'SendDonationsTo'.
Msg 207, Level 16, State 1, Line 9
Invalid column name 'DonationAddress'.
Msg 207, Level 16, State 1, Line 9
Invalid column name 'DonationCity'.
Msg 207, Level 16, State 1, Line 9
Invalid column name 'DonationState'.
Msg 207, Level 16, State 1, Line 9
Invalid column name 'DonationZip

Desikankannan

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-12 : 13:58:25
Hmm..You're adding columns and immediately trying to populate data from it. WHat does that mean? you'll just have some NULL values getting inserted in tbldonations.

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

desikankannan
Posting Yak Master

152 Posts

Posted - 2013-11-12 : 19:22:11
yes,

if the run script first time , it works fine, if the run again second
it throws the error, i don't want to show error message



quote:
Originally posted by visakh16

Hmm..You're adding columns and immediately trying to populate data from it. WHat does that mean? you'll just have some NULL values getting inserted in tbldonations.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs




Desikankannan
Go to Top of Page
   

- Advertisement -