Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Wednesday, March 28, 2012

Newbie Question - Please be gentle!

Can I run a script as an automated process?
i.e. I have a simple script that deletes the content of a table so I can
import fresh data, I know how to set up the DTS to import the data
automatically, but I have to run the script every day manually before the DTS
runs & I want the delete script to run at a predefined time rather than
having to remember to do it. Does that make sense?
Tia
Jonathan
Sure. It's hard to give precise directions without know what you're scripts
look like, but... if you already have a DTS job you can easily create a
step in that package (that's what the DTS container is called) that will cun
a TSQL script. Then you can easily schedule that from SQLAgent. You can
right click on the job name from DTS and select 'schedule job' which will
walk you throught the process of setting up the DTS package to run from SQL
agent.
Hope that helps,
Brian Moran
Principal Mentor
Solid Quality Learning
SQL Server MVP
http://www.solidqualitylearning.com
"Jonathan" <Jonathan@.discussions.microsoft.com> wrote in message
news:F7E842B8-BFD6-4922-8ED2-4AD0BCE55CA6@.microsoft.com...
> Can I run a script as an automated process?
> i.e. I have a simple script that deletes the content of a table so I can
> import fresh data, I know how to set up the DTS to import the data
> automatically, but I have to run the script every day manually before the
DTS
> runs & I want the delete script to run at a predefined time rather than
> having to remember to do it. Does that make sense?
> Tia
> Jonathan

Newbie Question - Please be gentle!

Can I run a script as an automated process?
i.e. I have a simple script that deletes the content of a table so I can
import fresh data, I know how to set up the DTS to import the data
automatically, but I have to run the script every day manually before the DT
S
runs & I want the delete script to run at a predefined time rather than
having to remember to do it. Does that make sense?
Tia
JonathanSure. It's hard to give precise directions without know what you're scripts
look like, but... if you already have a DTS job you can easily create a
step in that package (that's what the DTS container is called) that will cun
a TSQL script. Then you can easily schedule that from SQLAgent. You can
right click on the job name from DTS and select 'schedule job' which will
walk you throught the process of setting up the DTS package to run from SQL
agent.
Hope that helps,
Brian Moran
Principal Mentor
Solid Quality Learning
SQL Server MVP
http://www.solidqualitylearning.com
"Jonathan" <Jonathan@.discussions.microsoft.com> wrote in message
news:F7E842B8-BFD6-4922-8ED2-4AD0BCE55CA6@.microsoft.com...
> Can I run a script as an automated process?
> i.e. I have a simple script that deletes the content of a table so I can
> import fresh data, I know how to set up the DTS to import the data
> automatically, but I have to run the script every day manually before the
DTS
> runs & I want the delete script to run at a predefined time rather than
> having to remember to do it. Does that make sense?
> Tia
> Jonathan

Newbie Question - Please be gentle!

Can I run a script as an automated process?
i.e. I have a simple script that deletes the content of a table so I can
import fresh data, I know how to set up the DTS to import the data
automatically, but I have to run the script every day manually before the DTS
runs & I want the delete script to run at a predefined time rather than
having to remember to do it. Does that make sense?
Tia
JonathanSure. It's hard to give precise directions without know what you're scripts
look like, but... if you already have a DTS job you can easily create a
step in that package (that's what the DTS container is called) that will cun
a TSQL script. Then you can easily schedule that from SQLAgent. You can
right click on the job name from DTS and select 'schedule job' which will
walk you throught the process of setting up the DTS package to run from SQL
agent.
Hope that helps,
--
Brian Moran
Principal Mentor
Solid Quality Learning
SQL Server MVP
http://www.solidqualitylearning.com
"Jonathan" <Jonathan@.discussions.microsoft.com> wrote in message
news:F7E842B8-BFD6-4922-8ED2-4AD0BCE55CA6@.microsoft.com...
> Can I run a script as an automated process?
> i.e. I have a simple script that deletes the content of a table so I can
> import fresh data, I know how to set up the DTS to import the data
> automatically, but I have to run the script every day manually before the
DTS
> runs & I want the delete script to run at a predefined time rather than
> having to remember to do it. Does that make sense?
> Tia
> Jonathan

Monday, March 19, 2012

Newbie needing help with sql script

I keep getting this error on this script can some one help me out.

There is an error in the query. The data types varchar and sql_variant are incompatible in the add operator.
The multi-part identifier "LANGUAGES.DESCRIPTION" could not be bound.
The multi-part identifier "LANGUAGES.DESCRIPTION" could not be bound.


SELECT 'Quarter All' as 'qtr',
COUNT(JOB.JOBID) as 'transcount',
COUNT(DISTINCT JOB.PATIENTID) as 'patient count',
SUM(JOB.LANGUAGE_TCOST) as 'lcost',
SUM(JOB.LANGUAGE_DISC_COST) as 'dlcost',
AVG(JOB.LANGUAGE_DISC) as 'avgLDisc',
SUM(JOB.LANGUAGE_TCOST) + SUM(JOB.LANGUAGE_DISC_COST) as 'LGrossAmtBilled',
SUM(JOB.LANGUAGE_TCOST) / COUNT(DISTINCT JOB.PATIENTID) as 'PatAvgL',
SUM(JOB.LANGUAGE_TCOST) / COUNT(JOB.JOBID) as 'RefAvgL',
SUM(JOB.LANGUAGE_DISC) as 'avgPercentDiscL',
JOB.JURISDICTION,
PAYER.PAY_COMPANY,
PAYER.PAY_CITY,
PAYER.PAY_STATE,
PAYER.PAY_SALES_STAFF_ID,
JOB.INVOICE_DATE,
JOB.JOBOUTCOMEID,
JOB.SERVICEOUTCOME,
INVOICE_AR.INVOICE_NO,
INVOICE_AR.INVOICE_DATE AS EXPR1,
INVOICE_AR.AMOUNT_DUE,
INVOICE_AR.CLAIMNUMBER,
LANGUAGES.DESCRIPTION

FROM JOB
INNER JOIN INVOICE_AR
ON JOB.JOBID = INVOICE_AR.JOBID
LEFT OUTER JOIN PAYER
ON PAYER.PAYERID = JOB.PAYERID
LEFT OUTER JOIN STATES
ON JOB.JURISDICTION = STATES.INITIALS

WHERE
(INVOICE_AR.AMOUNT_DUE > 0)
AND
(INVOICE_AR.INVOICE_DATE BETWEEN @.startdate and @.enddate)
AND
(MONTH(INVOICE_AR.INVOICE_DATE) IN (1,2,3,4,5,6,7,8,9,10,11,12))
AND
(PAYER.PAY_COMPANY like '%' + @.Company + '%')


Group By
JOB.JURISDICTION,
PAYER.PAY_COMPANY,
PAYER.PAY_CITY,
PAYER.PAY_STATE,
PAYER.PAY_SALES_STAFF_ID,
JOB.INVOICE_DATE,
JOB.JOBOUTCOMEID,
JOB.SERVICEOUTCOME,
INVOICE_AR.INVOICE_NO,
INVOICE_AR.INVOICE_DATE,
INVOICE_AR.AMOUNT_DUE,
INVOICE_AR.CLAIMNUMBER,
LANGUAGES.DESCRIPTION
Order By 'QTR' asc

You have Languages.Description in your select list and your group expression but I don't see where you have specified Languages as a table in your join expression. Otherwise, I don't know your table design so I don't know where you are trying to add a varchar and a sql_variant.|||

I am able to run this script in my sql 2005 report but when i go from the design mode to the preview mode I get this error.

An error occurred during reporting processing

cannot read the next data row for data set al2

conversion failed when converting the nvarchar value 'AARIYA' to data type int

the only changes I have made to this script is adding the JOB.LANUAGE_ID AND THE LANGUAGE.DESCRIPTION AND THE LEFT JION STATEMENT FOR THEM

SELECT 'Quarter All' as 'qtr',
COUNT(JOB.JOBID) as 'transcount',
COUNT(DISTINCT JOB.PATIENTID) as 'patient count',
SUM(JOB.LANGUAGE_TCOST) as 'lcost',
SUM(JOB.LANGUAGE_DISC_COST) as 'dlcost',
AVG(JOB.LANGUAGE_DISC) as 'avgLDisc',
SUM(JOB.LANGUAGE_TCOST) + SUM(JOB.LANGUAGE_DISC_COST) as 'LGrossAmtBilled',
SUM(JOB.LANGUAGE_TCOST) / COUNT(DISTINCT JOB.PATIENTID) as 'PatAvgL',
SUM(JOB.LANGUAGE_TCOST) / COUNT(JOB.JOBID) as 'RefAvgL',
SUM(JOB.LANGUAGE_DISC) as 'avgPercentDiscL',
JOB.JURISDICTION,
PAYER.PAY_COMPANY,
PAYER.PAY_CITY,
PAYER.PAY_STATE,
PAYER.PAY_SALES_STAFF_ID,
JOB.INVOICE_DATE,
JOB.JOBOUTCOMEID,
JOB.SERVICEOUTCOME,
JOB.LANGUAGE_ID,
INVOICE_AR.INVOICE_NO,
INVOICE_AR.INVOICE_DATE AS EXPR1,
INVOICE_AR.AMOUNT_DUE,
INVOICE_AR.CLAIMNUMBER,
LANGUAGES.DESCRIPTION

FROM JOB
INNER JOIN INVOICE_AR
ON JOB.JOBID = INVOICE_AR.JOBID
LEFT OUTER JOIN PAYER
ON PAYER.PAYERID = JOB.PAYERID
LEFT OUTER JOIN STATES
ON JOB.JURISDICTION = STATES.INITIALS
LEFT OUTER JOIN LANGUAGES
ON JOB.LANGUAGE_ID = LANGUAGES.DESCRIPTION

WHERE
(INVOICE_AR.AMOUNT_DUE > 0)
AND
(INVOICE_AR.INVOICE_DATE BETWEEN @.startdate and @.enddate)
AND
(MONTH(INVOICE_AR.INVOICE_DATE) IN (1,2,3,4,5,6,7,8,9,10,11,12))
AND
(PAYER.PAY_COMPANY like '%' + @.Company + '%')


Group By
JOB.JURISDICTION,
PAYER.PAY_COMPANY,
PAYER.PAY_CITY,
PAYER.PAY_STATE,
PAYER.PAY_SALES_STAFF_ID,
JOB.INVOICE_DATE,
JOB.JOBOUTCOMEID,
JOB.SERVICEOUTCOME,
JOB.LANGUAGE_ID,
INVOICE_AR.INVOICE_NO,
INVOICE_AR.INVOICE_DATE,
INVOICE_AR.AMOUNT_DUE,
INVOICE_AR.CLAIMNUMBER,
LANGUAGES.DESCRIPTION


Order By 'QTR' asc

|||

Is that value coming through this parameter @.Company? What are the values that you are passing to @.Company? and what is the datatype of PAY_COMPANY?

Also, is this really required? Can there be a month thats not from this list?

(MONTH(INVOICE_AR.INVOICE_DATE) IN (1,2,3,4,5,6,7,8,9,10,11,12))

|||

First I ahave a more complex tsql query I just took the first part of it I have it breaking down to quarters, weeks,and days. Evething in the query worked until I add the join statement and the 2 fields in the grouping mentioned above. I wiil past what works firs below and then what doesnt work.

This works

SELECT 'Quarter All' as 'qtr',
COUNT(JOB.JOBID) as 'transcount',
COUNT(DISTINCT JOB.PATIENTID) as 'patient count',
SUM(JOB.LANGUAGE_TCOST) as 'lcost',
SUM(JOB.LANGUAGE_DISC_COST) as 'dlcost',
AVG(JOB.LANGUAGE_DISC) as 'avgLDisc',
SUM(JOB.LANGUAGE_TCOST) + SUM(JOB.LANGUAGE_DISC_COST) as 'LGrossAmtBilled',
SUM(JOB.LANGUAGE_TCOST) / COUNT(DISTINCT JOB.PATIENTID) as 'PatAvgL',
SUM(JOB.LANGUAGE_TCOST) / COUNT(JOB.JOBID) as 'RefAvgL',
SUM(JOB.LANGUAGE_DISC) as 'avgPercentDiscL',
JOB.JURISDICTION,
PAYER.PAY_COMPANY,
PAYER.PAY_CITY,
PAYER.PAY_STATE,
PAYER.PAY_SALES_STAFF_ID,
JOB.INVOICE_DATE,
JOB.JOBOUTCOMEID,
JOB.SERVICEOUTCOME,
INVOICE_AR.INVOICE_NO,
INVOICE_AR.INVOICE_DATE AS EXPR1,
INVOICE_AR.AMOUNT_DUE,
INVOICE_AR.CLAIMNUMBER

FROM JOB
INNER JOIN INVOICE_AR
ON JOB.JOBID = INVOICE_AR.JOBID
LEFT OUTER JOIN PAYER
ON PAYER.PAYERID = JOB.PAYERID
LEFT OUTER JOIN STATES
ON JOB.JURISDICTION = STATES.INITIALS

WHERE
(INVOICE_AR.AMOUNT_DUE > 0)
AND
(INVOICE_AR.INVOICE_DATE BETWEEN @.startdate and @.enddate)
AND
(MONTH(INVOICE_AR.INVOICE_DATE) IN (1,2,3,4,5,6,7,8,9,10,11,12))
AND
(PAYER.PAY_COMPANY like '%' + @.Company + '%')


Group By
JOB.JURISDICTION,
PAYER.PAY_COMPANY,
PAYER.PAY_CITY,
PAYER.PAY_STATE,
PAYER.PAY_SALES_STAFF_ID,
JOB.INVOICE_DATE,
JOB.JOBOUTCOMEID,
JOB.SERVICEOUTCOME,
INVOICE_AR.INVOICE_NO,
INVOICE_AR.INVOICE_DATE,
INVOICE_AR.AMOUNT_DUE,
INVOICE_AR.CLAIMNUMBER

ORDER BY 'QTR' asc

The below query doesnt work gives the error listed below when I go to preview in the sql reports

There is an error in the query. The data types varchar and sql_variant are incompatible in the add operator.
The multi-part identifier "LANGUAGES.DESCRIPTION" could not be bound.
The multi-part identifier "LANGUAGES.DESCRIPTION" could not be bound.


SELECT 'Quarter All' as 'qtr',
COUNT(JOB.JOBID) as 'transcount',
COUNT(DISTINCT JOB.PATIENTID) as 'patient count',
SUM(JOB.LANGUAGE_TCOST) as 'lcost',
SUM(JOB.LANGUAGE_DISC_COST) as 'dlcost',
AVG(JOB.LANGUAGE_DISC) as 'avgLDisc',
SUM(JOB.LANGUAGE_TCOST) + SUM(JOB.LANGUAGE_DISC_COST) as 'LGrossAmtBilled',
SUM(JOB.LANGUAGE_TCOST) / COUNT(DISTINCT JOB.PATIENTID) as 'PatAvgL',
SUM(JOB.LANGUAGE_TCOST) / COUNT(JOB.JOBID) as 'RefAvgL',
SUM(JOB.LANGUAGE_DISC) as 'avgPercentDiscL',
JOB.JURISDICTION,
PAYER.PAY_COMPANY,
PAYER.PAY_CITY,
PAYER.PAY_STATE,
PAYER.PAY_SALES_STAFF_ID,
JOB.INVOICE_DATE,
JOB.JOBOUTCOMEID,
JOB.SERVICEOUTCOME,
JOB.LANGUAGE_ID,
INVOICE_AR.INVOICE_NO,
INVOICE_AR.INVOICE_DATE AS EXPR1,
INVOICE_AR.AMOUNT_DUE,
INVOICE_AR.CLAIMNUMBER,
LANGUAGES.DESCRIPTION

FROM JOB
INNER JOIN INVOICE_AR
ON JOB.JOBID = INVOICE_AR.JOBID
LEFT OUTER JOIN PAYER
ON PAYER.PAYERID = JOB.PAYERID
LEFT OUTER JOIN STATES
ON JOB.JURISDICTION = STATES.INITIALS
LEFT OUTER JOIN LANGUAGES
ON JOB.LANGUAGE_ID = LANGUAGES.DESCRIPTION

WHERE
(INVOICE_AR.AMOUNT_DUE > 0)
AND
(INVOICE_AR.INVOICE_DATE BETWEEN @.startdate and @.enddate)
AND
(MONTH(INVOICE_AR.INVOICE_DATE) IN (1,2,3,4,5,6,7,8,9,10,11,12))
AND
(PAYER.PAY_COMPANY like '%' + @.Company + '%')


Group By
JOB.JURISDICTION,
PAYER.PAY_COMPANY,
PAYER.PAY_CITY,
PAYER.PAY_STATE,
PAYER.PAY_SALES_STAFF_ID,
JOB.INVOICE_DATE,
JOB.JOBOUTCOMEID,
JOB.SERVICEOUTCOME,
JOB.LANGUAGE_ID,
INVOICE_AR.INVOICE_NO,
INVOICE_AR.INVOICE_DATE,
INVOICE_AR.AMOUNT_DUE,
INVOICE_AR.CLAIMNUMBER,
LANGUAGES.DESCRIPTION
Order By 'QTR' asc

Saturday, February 25, 2012

Newbie - can I do this in a script

I would like my script to check for the existance of a table, and if found
make sure theres enuf dispace back it up (otherwise display a message and
halt), then I need to alter a table. If it doesn't exist I would like to
create it like normal.
I've been trolling thru the sql help files but can't find how to check for
existence of a table. Any pointers/articles someone can get me too?
JeffYou can check the existence of a table using the following syntax:
IF OBJECT_ID('database.object_owner.object') IS NOT NULL
BEGIN
PRINT 'object exists'
END
eg. Checking if the Northwind Orders table exists
IF OBJECT_ID('northwind.dbo.orders') IS NOT NULL
BEGIN
PRINT 'object exists'
END
- Peter Ward
WARDY IT Solutions
"J. Clarke" wrote:

> I would like my script to check for the existance of a table, and if found
> make sure theres enuf dispace back it up (otherwise display a message and
> halt), then I need to alter a table. If it doesn't exist I would like to
> create it like normal.
> I've been trolling thru the sql help files but can't find how to check for
> existence of a table. Any pointers/articles someone can get me too?
> Jeff
>
>|||Thanks Peter - I guess you answered my followup question about "if"
statements. How would I format a user defined procedure or a function?
Jeff
"P. Ward" <peter@.remove_online.wardyit.com> wrote in message
news:BD0B1E97-FE1F-4A38-89A9-A53385DBFCDC@.microsoft.com...
> You can check the existence of a table using the following syntax:
> IF OBJECT_ID('database.object_owner.object') IS NOT NULL
> BEGIN
> PRINT 'object exists'
> END
> eg. Checking if the Northwind Orders table exists
> IF OBJECT_ID('northwind.dbo.orders') IS NOT NULL
> BEGIN
> PRINT 'object exists'
> END|||Will this work on just the db also (i.e. northwind)?
Jeff
"P. Ward" <peter@.remove_online.wardyit.com> wrote in message
news:BD0B1E97-FE1F-4A38-89A9-A53385DBFCDC@.microsoft.com...
> You can check the existence of a table using the following syntax:
> IF OBJECT_ID('database.object_owner.object') IS NOT NULL
> BEGIN
> PRINT 'object exists'
> END
> eg. Checking if the Northwind Orders table exists
> IF OBJECT_ID('northwind.dbo.orders') IS NOT NULL
> BEGIN
> PRINT 'object exists'
> END|||Jeff
I am not sure what you are asking, can you please clarify your question.
Thanks
- Peter Ward
WARDY IT Solutions
"J. Clarke" wrote:

> Will this work on just the db also (i.e. northwind)?
> Jeff
> "P. Ward" <peter@.remove_online.wardyit.com> wrote in message
> news:BD0B1E97-FE1F-4A38-89A9-A53385DBFCDC@.microsoft.com...
>
>|||Sorry Ward - the question is can I check for the existance of a DB (vs. the
table)?
I think I tried something like: IF OBJECT_ID('database') IS NOT NULL and
the QA didn't seem to like it. I guess it doesn't matter too much since
that table will always be there if the DB is...
Jeff
"P. Ward" <peter@.remove_online.wardyit.com> wrote in message
news:F576737F-8C16-4D87-B830-7D3A95960907@.microsoft.com...
> Jeff
> I am not sure what you are asking, can you please clarify your question.
> Thanks
>
> - Peter Ward|||Use DB_ID() for that:
IF DB_ID('pubs') IS NOT NULL
PRINT 'Database exists'
ELSE
PRINT 'Database doesn''t exists'
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"J. Clarke" <jclarke@.docstorsysNOSPAM.com> wrote in message
news:O7KMBXHMFHA.508@.TK2MSFTNGP12.phx.gbl...
> Sorry Ward - the question is can I check for the existance of a DB (vs. th
e
> table)?
> I think I tried something like: IF OBJECT_ID('database') IS NOT NULL and
> the QA didn't seem to like it. I guess it doesn't matter too much since
> that table will always be there if the DB is...
> Jeff
>
> "P. Ward" <peter@.remove_online.wardyit.com> wrote in message
> news:F576737F-8C16-4D87-B830-7D3A95960907@.microsoft.com...
>

Newbie - can I do this in a script

I would like my script to check for the existance of a table, and if found
make sure theres enuf dispace back it up (otherwise display a message and
halt), then I need to alter a table. If it doesn't exist I would like to
create it like normal.
I've been trolling thru the sql help files but can't find how to check for
existence of a table. Any pointers/articles someone can get me too?
JeffYou can check the existence of a table using the following syntax:
IF OBJECT_ID('database.object_owner.object') IS NOT NULL
BEGIN
PRINT 'object exists'
END
eg. Checking if the Northwind Orders table exists
IF OBJECT_ID('northwind.dbo.orders') IS NOT NULL
BEGIN
PRINT 'object exists'
END
- Peter Ward
WARDY IT Solutions
"J. Clarke" wrote:
> I would like my script to check for the existance of a table, and if found
> make sure theres enuf dispace back it up (otherwise display a message and
> halt), then I need to alter a table. If it doesn't exist I would like to
> create it like normal.
> I've been trolling thru the sql help files but can't find how to check for
> existence of a table. Any pointers/articles someone can get me too?
> Jeff
>
>|||Thanks Peter - I guess you answered my followup question about "if"
statements. How would I format a user defined procedure or a function?
Jeff
"P. Ward" <peter@.remove_online.wardyit.com> wrote in message
news:BD0B1E97-FE1F-4A38-89A9-A53385DBFCDC@.microsoft.com...
> You can check the existence of a table using the following syntax:
> IF OBJECT_ID('database.object_owner.object') IS NOT NULL
> BEGIN
> PRINT 'object exists'
> END
> eg. Checking if the Northwind Orders table exists
> IF OBJECT_ID('northwind.dbo.orders') IS NOT NULL
> BEGIN
> PRINT 'object exists'
> END|||Will this work on just the db also (i.e. northwind)?
Jeff
"P. Ward" <peter@.remove_online.wardyit.com> wrote in message
news:BD0B1E97-FE1F-4A38-89A9-A53385DBFCDC@.microsoft.com...
> You can check the existence of a table using the following syntax:
> IF OBJECT_ID('database.object_owner.object') IS NOT NULL
> BEGIN
> PRINT 'object exists'
> END
> eg. Checking if the Northwind Orders table exists
> IF OBJECT_ID('northwind.dbo.orders') IS NOT NULL
> BEGIN
> PRINT 'object exists'
> END|||Jeff
I am not sure what you are asking, can you please clarify your question.
Thanks
- Peter Ward
WARDY IT Solutions
"J. Clarke" wrote:
> Will this work on just the db also (i.e. northwind)?
> Jeff
> "P. Ward" <peter@.remove_online.wardyit.com> wrote in message
> news:BD0B1E97-FE1F-4A38-89A9-A53385DBFCDC@.microsoft.com...
> > You can check the existence of a table using the following syntax:
> >
> > IF OBJECT_ID('database.object_owner.object') IS NOT NULL
> > BEGIN
> > PRINT 'object exists'
> > END
> >
> > eg. Checking if the Northwind Orders table exists
> >
> > IF OBJECT_ID('northwind.dbo.orders') IS NOT NULL
> > BEGIN
> > PRINT 'object exists'
> > END
>
>|||Sorry Ward - the question is can I check for the existance of a DB (vs. the
table)?
I think I tried something like: IF OBJECT_ID('database') IS NOT NULL and
the QA didn't seem to like it. I guess it doesn't matter too much since
that table will always be there if the DB is...
Jeff
"P. Ward" <peter@.remove_online.wardyit.com> wrote in message
news:F576737F-8C16-4D87-B830-7D3A95960907@.microsoft.com...
> Jeff
> I am not sure what you are asking, can you please clarify your question.
> Thanks
>
> - Peter Ward|||Use DB_ID() for that:
IF DB_ID('pubs') IS NOT NULL
PRINT 'Database exists'
ELSE
PRINT 'Database doesn''t exists'
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"J. Clarke" <jclarke@.docstorsysNOSPAM.com> wrote in message
news:O7KMBXHMFHA.508@.TK2MSFTNGP12.phx.gbl...
> Sorry Ward - the question is can I check for the existance of a DB (vs. the
> table)?
> I think I tried something like: IF OBJECT_ID('database') IS NOT NULL and
> the QA didn't seem to like it. I guess it doesn't matter too much since
> that table will always be there if the DB is...
> Jeff
>
> "P. Ward" <peter@.remove_online.wardyit.com> wrote in message
> news:F576737F-8C16-4D87-B830-7D3A95960907@.microsoft.com...
>> Jeff
>> I am not sure what you are asking, can you please clarify your question.
>> Thanks
>>
>> - Peter Ward
>

Newbie - can I do this in a script

I would like my script to check for the existance of a table, and if found
make sure theres enuf dispace back it up (otherwise display a message and
halt), then I need to alter a table. If it doesn't exist I would like to
create it like normal.
I've been trolling thru the sql help files but can't find how to check for
existence of a table. Any pointers/articles someone can get me too?
Jeff
You can check the existence of a table using the following syntax:
IF OBJECT_ID('database.object_owner.object') IS NOT NULL
BEGIN
PRINT 'object exists'
END
eg. Checking if the Northwind Orders table exists
IF OBJECT_ID('northwind.dbo.orders') IS NOT NULL
BEGIN
PRINT 'object exists'
END
- Peter Ward
WARDY IT Solutions
"J. Clarke" wrote:

> I would like my script to check for the existance of a table, and if found
> make sure theres enuf dispace back it up (otherwise display a message and
> halt), then I need to alter a table. If it doesn't exist I would like to
> create it like normal.
> I've been trolling thru the sql help files but can't find how to check for
> existence of a table. Any pointers/articles someone can get me too?
> Jeff
>
>
|||Thanks Peter - I guess you answered my followup question about "if"
statements. How would I format a user defined procedure or a function?
Jeff
"P. Ward" <peter@.remove_online.wardyit.com> wrote in message
news:BD0B1E97-FE1F-4A38-89A9-A53385DBFCDC@.microsoft.com...
> You can check the existence of a table using the following syntax:
> IF OBJECT_ID('database.object_owner.object') IS NOT NULL
> BEGIN
> PRINT 'object exists'
> END
> eg. Checking if the Northwind Orders table exists
> IF OBJECT_ID('northwind.dbo.orders') IS NOT NULL
> BEGIN
> PRINT 'object exists'
> END
|||Will this work on just the db also (i.e. northwind)?
Jeff
"P. Ward" <peter@.remove_online.wardyit.com> wrote in message
news:BD0B1E97-FE1F-4A38-89A9-A53385DBFCDC@.microsoft.com...
> You can check the existence of a table using the following syntax:
> IF OBJECT_ID('database.object_owner.object') IS NOT NULL
> BEGIN
> PRINT 'object exists'
> END
> eg. Checking if the Northwind Orders table exists
> IF OBJECT_ID('northwind.dbo.orders') IS NOT NULL
> BEGIN
> PRINT 'object exists'
> END
|||Jeff
I am not sure what you are asking, can you please clarify your question.
Thanks
- Peter Ward
WARDY IT Solutions
"J. Clarke" wrote:

> Will this work on just the db also (i.e. northwind)?
> Jeff
> "P. Ward" <peter@.remove_online.wardyit.com> wrote in message
> news:BD0B1E97-FE1F-4A38-89A9-A53385DBFCDC@.microsoft.com...
>
>
|||Sorry Ward - the question is can I check for the existance of a DB (vs. the
table)?
I think I tried something like: IF OBJECT_ID('database') IS NOT NULL and
the QA didn't seem to like it. I guess it doesn't matter too much since
that table will always be there if the DB is...
Jeff
"P. Ward" <peter@.remove_online.wardyit.com> wrote in message
news:F576737F-8C16-4D87-B830-7D3A95960907@.microsoft.com...
> Jeff
> I am not sure what you are asking, can you please clarify your question.
> Thanks
>
> - Peter Ward
|||Use DB_ID() for that:
IF DB_ID('pubs') IS NOT NULL
PRINT 'Database exists'
ELSE
PRINT 'Database doesn''t exists'
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"J. Clarke" <jclarke@.docstorsysNOSPAM.com> wrote in message
news:O7KMBXHMFHA.508@.TK2MSFTNGP12.phx.gbl...
> Sorry Ward - the question is can I check for the existance of a DB (vs. the
> table)?
> I think I tried something like: IF OBJECT_ID('database') IS NOT NULL and
> the QA didn't seem to like it. I guess it doesn't matter too much since
> that table will always be there if the DB is...
> Jeff
>
> "P. Ward" <peter@.remove_online.wardyit.com> wrote in message
> news:F576737F-8C16-4D87-B830-7D3A95960907@.microsoft.com...
>