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
 If not exists

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2010-09-16 : 11:14:39
I want to run the following script only if records do not exist in the table already. So that the script can be ran over and over again with no errors.


insert into sydocfil_sql(
sydoc_number,
sydoc_document_path)
select sysnotes_sql.doc_aware_1,
'C:\Pubs\PDA Quote\_current\'+Replace(ltrim(Replace(sysnotes_sql.name_value,'0',' ')),' ','0')+'.xls'
FROM sysnotes_sql
where sysnotes_sql.note_topic = 'PDA'

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-09-16 : 11:23:36
insert into sydocfil_sql(
sydoc_number,
sydoc_document_path)
select t.doc_aware_1,
'C:\Pubs\PDA Quote\_current\'+Replace(ltrim(Replace(t.name_value,'0',' ')),' ','0')+'.xls'
FROM sysnotes_sql as t
where t.note_topic = 'PDA'
and not exists(select * from sydocfil_sql t1 where t1.sydoc_number = t.doc_aware_1)


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

rohitvishwakarma
Posting Yak Master

232 Posts

Posted - 2010-09-16 : 11:27:05
IF NOT EXISTS(
select sysnotes_sql.doc_aware_1,
'C:\Pubs\PDA Quote\_current\'+Replace(ltrim(Replace(sysnotes_sql.name_value,'0',' ')),' ','0')+'.xls'
FROM sysnotes_sql
where sysnotes_sql.note_topic = 'PDA'
)
BEGIN

insert into sydocfil_sql(
sydoc_number,
sydoc_document_path)
select sysnotes_sql.doc_aware_1,
'C:\Pubs\PDA Quote\_current\'+Replace(ltrim(Replace(sysnotes_sql.name_value,'0',' ')),' ','0')+'.xls'
FROM sysnotes_sql
where sysnotes_sql.note_topic = 'PDA'

END
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-09-16 : 11:45:46
quote:
Originally posted by rohitvishwakarma

IF NOT EXISTS(
select sysnotes_sql.doc_aware_1,
'C:\Pubs\PDA Quote\_current\'+Replace(ltrim(Replace(sysnotes_sql.name_value,'0',' ')),' ','0')+'.xls'
FROM sysnotes_sql
where sysnotes_sql.note_topic = 'PDA'
)
BEGIN

insert into sydocfil_sql(
sydoc_number,
sydoc_document_path)
select sysnotes_sql.doc_aware_1,
'C:\Pubs\PDA Quote\_current\'+Replace(ltrim(Replace(sysnotes_sql.name_value,'0',' ')),' ','0')+'.xls'
FROM sysnotes_sql
where sysnotes_sql.note_topic = 'PDA'

END


This will not work because you are checking the wrong table.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

rohitvishwakarma
Posting Yak Master

232 Posts

Posted - 2010-09-16 : 11:48:19
quote:
Originally posted by webfred

quote:
Originally posted by rohitvishwakarma

IF NOT EXISTS(
select sysnotes_sql.doc_aware_1,
'C:\Pubs\PDA Quote\_current\'+Replace(ltrim(Replace(sysnotes_sql.name_value,'0',' ')),' ','0')+'.xls'
FROM sysnotes_sql
where sysnotes_sql.note_topic = 'PDA'
)
BEGIN

insert into sydocfil_sql(
sydoc_number,
sydoc_document_path)
select sysnotes_sql.doc_aware_1,
'C:\Pubs\PDA Quote\_current\'+Replace(ltrim(Replace(sysnotes_sql.name_value,'0',' ')),' ','0')+'.xls'
FROM sysnotes_sql
where sysnotes_sql.note_topic = 'PDA'

END


This will not work because you are checking the wrong table.


No, you're never too old to Yak'n'Roll if you're too young to die.



oops i missed this one
Thanks
Go to Top of Page
   

- Advertisement -