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 2005 Forums
 Transact-SQL (2005)
 Insert Statement Help

Author  Topic 

JeffS23
Posting Yak Master

212 Posts

Posted - 2008-06-09 : 13:49:09
I need help writing an update script in SQL 2005. My client sent me a list of codes in an excel file they would like to add to these tables. The excel sheet is not big at all. I need to write in my update script somehow to first check if these values exist and if not, update the tables. Any help is greatly appreciated. I would perfer not importing the excel sheet if possible. I had to set the SET IDENTITY INSERT ON to just enter this in my demo database. Any insight on this too would be helpful


SET IDENTITY_INSERT cusImmunCodes ON
INSERT INTO cusImmunCodes
(
cusImmunCodesID,
Code,
Description,
CatCode,
Created,
CreatedBy,
LastModified,
LastModifiedBy
)
VALUES (
/* cusImmunCodesID - int */ 84,
/* Code - varchar(10) */ '90636',
/* Description - varchar(35) */ 'Hep A & B (adult dose)',
/* CatCode - int */ 42,
/* Created - datetime */ GETDATE(),
/* CreatedBy - varchar(30) */ 'CYS_SCRIPT',
/* LastModified - datetime */ GETDATE(),
/* LastModifiedBy - varchar(30) */ 'CYS_SCRIPT'
)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-09 : 13:49:49
What is your question?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

JeffS23
Posting Yak Master

212 Posts

Posted - 2008-06-09 : 13:51:54
I hit enter to quick ... question posted now.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-09 : 14:06:44
[code]IF NOT EXISTS (SELECT * FROM cusImmunCodes WHERE cusImmunCodesID=@ID)
BEGIN
INSERT INTO cusImmunCodes
(
cusImmunCodesID,
Code,
Description,
CatCode,
Created,
CreatedBy,
LastModified,
LastModifiedBy
)
VALUES (@ID,
othervalues...
)
END
ELSE
BEGIN
UPDATE cusImmunCodes
SET Code=value2,
Description=value3,...
WHERE cusImmunCodesID=@ID
END[/code]
Go to Top of Page
   

- Advertisement -