Showing posts with label exists. Show all posts
Showing posts with label exists. Show all posts

Tuesday, March 27, 2012

Delete an excel worksheet only if it exists, in a dts package

Hi :

1. I import data into sqlserver, from an excel file, from a worksheet
within it, named 'input_data'
2. then i scrub the data in sqlserver
3. then i export some data into the same excelfile but into another
worksheet named 'output_data'

I have got it working and this is how i am doing it right now. I am
having a problem in the few steps identified below :

1. I delete the worksheet 'output_data' using an execsql task
DROP TABLE `output_data'

2. Then i have another execsql task to create it
CREATE TABLE `output_data`

3. Then i have datapump task to put data into worksheet.

The work fine, but i am running into a problem, when the output_data
worksheet doesnt exist.
So i need to write a query telling
IF `output_data` doesnt exist, then DROP IT

How do i do this
I am open to doing this in ActiveX Script(vbscript) and ExecSQL task

Also i do not have excel dll or runtime in my dbserver.

Please let me know
thanks
RSapologize for typo
I do have a problem, when 'output_data' worksheet does exist
and hence need a query telling,

if output_data worksheet exists, then drop it

thank you
RS

Thursday, March 22, 2012

Delete - Exists - problem

Can anyone tell me why the following deletes all the records instead of
simply the ones returned in the "Exists" clause?
The select in the exists by itself returns 131 records, however when run in
the following context it deletes all 4474 that are in the Shades table.'
Delete FROM #TMP_SHADE WHERE EXISTS
(select DISTINCT(OLD_SHADE_ID) from #TMP_CHANGEDSHADES
where OLD_SHADE_ID NOT in (select SHADE_ID
from SHADE_SUC))
The idea is to delete any shade records in #tmp_shade where they do not
exist in shade_SUC
ThanksDan,

> Can anyone tell me why the following deletes all the records instead of
> simply the ones returned in the "Exists" clause?
Because you are not correlating the tables. If at least one row in table
#TMP_CHANGEDSHADES meet the condition, all rows from table #TMP_SHADE will b
e
deleted. It should be something like:
Delete
FROM #TMP_SHADE
WHERE EXISTS
(
select DISTINCT(OLD_SHADE_ID)
from #TMP_CHANGEDSHADES
where #TMP_CHANGEDSHADES.col1 = #TMP_SHADE.col1
and OLD_SHADE_ID NOT in (select SHADE_ID from SHADE_SUC)
)
AMB
"Dan" wrote:

> Can anyone tell me why the following deletes all the records instead of
> simply the ones returned in the "Exists" clause?
> The select in the exists by itself returns 131 records, however when run i
n
> the following context it deletes all 4474 that are in the Shades table.?
?
> Delete FROM #TMP_SHADE WHERE EXISTS
> (select DISTINCT(OLD_SHADE_ID) from #TMP_CHANGEDSHADES
> where OLD_SHADE_ID NOT in (select SHADE_ID
> from SHADE_SUC))
> The idea is to delete any shade records in #tmp_shade where they do not
> exist in shade_SUC
> Thanks
>|||Delete from X where Exists (Y)
will delete all rows in X if Y is true and nothing if Y is false.
try something like
Delete from X where Y_id in (select distinct(Y_id) from Y)
I hope you get the idea.
Regards,
Nishant
"Dan" wrote:

> Can anyone tell me why the following deletes all the records instead of
> simply the ones returned in the "Exists" clause?
> The select in the exists by itself returns 131 records, however when run i
n
> the following context it deletes all 4474 that are in the Shades table.?
?
> Delete FROM #TMP_SHADE WHERE EXISTS
> (select DISTINCT(OLD_SHADE_ID) from #TMP_CHANGEDSHADES
> where OLD_SHADE_ID NOT in (select SHADE_ID
> from SHADE_SUC))
> The idea is to delete any shade records in #tmp_shade where they do not
> exist in shade_SUC
> Thanks
>|||Great Alejandro! - Thank you for the pointer
Dan
"Alejandro Mesa" wrote:
> Dan,
>
> Because you are not correlating the tables. If at least one row in table
> #TMP_CHANGEDSHADES meet the condition, all rows from table #TMP_SHADE will
be
> deleted. It should be something like:
> Delete
> FROM #TMP_SHADE
> WHERE EXISTS
> (
> select DISTINCT(OLD_SHADE_ID)
> from #TMP_CHANGEDSHADES
> where #TMP_CHANGEDSHADES.col1 = #TMP_SHADE.col1
> and OLD_SHADE_ID NOT in (select SHADE_ID from SHADE_SUC)
> )
>
> AMB
>
>
> "Dan" wrote:
>sql

Friday, February 24, 2012

Default values

Is there a way to check to see if a default value exists for an existing
column in a table using SQL script? If so, how?http://www.aspfaq.com/2177
--
http://www.aspfaq.com/
(Reverse address to reply.)
"Robert" <netsec4u@.hotmail.com> wrote in message
news:OHQx#K9eEHA.2804@.TK2MSFTNGP11.phx.gbl...
> Is there a way to check to see if a default value exists for an existing
> column in a table using SQL script? If so, how?
>|||SELECT column_default
FROM information_schema.columns
WHERE table_name = 'TABLE_NAME'
AND column_name = 'COLUMN_NAME'
--
David Portas
SQL Server MVP
--

Default values

Is there a way to check to see if a default value exists for an existing
column in a table using SQL script? If so, how?
http://www.aspfaq.com/2177
http://www.aspfaq.com/
(Reverse address to reply.)
"Robert" <netsec4u@.hotmail.com> wrote in message
news:OHQx#K9eEHA.2804@.TK2MSFTNGP11.phx.gbl...
> Is there a way to check to see if a default value exists for an existing
> column in a table using SQL script? If so, how?
>
|||SELECT column_default
FROM information_schema.columns
WHERE table_name = 'TABLE_NAME'
AND column_name = 'COLUMN_NAME'
David Portas
SQL Server MVP

Default values

Is there a way to check to see if a default value exists for an existing
column in a table using SQL script? If so, how?http://www.aspfaq.com/2177
http://www.aspfaq.com/
(Reverse address to reply.)
"Robert" <netsec4u@.hotmail.com> wrote in message
news:OHQx#K9eEHA.2804@.TK2MSFTNGP11.phx.gbl...
> Is there a way to check to see if a default value exists for an existing
> column in a table using SQL script? If so, how?
>|||SELECT column_default
FROM information_schema.columns
WHERE table_name = 'TABLE_NAME'
AND column_name = 'COLUMN_NAME'
David Portas
SQL Server MVP
--