| Author |
Topic |
|
Bjcascone
Starting Member
37 Posts |
Posted - 2010-08-12 : 14:57:46
|
| I am working on a Stored Proc that inserts some selected data from a set group of tables. the issue is that some times some of these tables do not exist. if the tables do not exist and the SP is run it will error out before completion. some of the script is hard coded. but here is what i have so far:Case when [JcpDataOraclePoLines].[dbo].[PoLinesAll_Load] exists then INSERT INTO Jcp..StratificationLoadTablesSelect 2608,-- AS AuditId, 'POHeadersLines', 'LastUpdateDate', 'ExtAmt', Year(A.LastUpdateDate) as YY, Month(A.LastUpdateDate) as MM, Day(A.LastUpdateDate) as DD, Sum(CAST(ROUND(([UnitPrice] *[Quantity]), 2) as Money)) as Volume, Sum(1) as TotalFROM JcpDataOraclePO.dbo.POHeadersAll_load AJOIN [JcpDataOraclePoLines].[dbo].[PoLinesAll_Load] BON A.PoHeaderId = B.PoHEaderIdGroup By Year(A.LastUpdateDate), Month(A.LastUpdateDate),Day(A.LastUpdateDate)Order By Year(A.LastUpdateDate), Month(A.LastUpdateDate),Day(A.LastUpdateDate)ElseBasically i would like to preface this with an If Table exists clause but i am not sure how the syntax would go. any help or alternative ideas would be helpfulThanks |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-12 : 15:29:22
|
| it seems like it may be part of job batch in which one step is dropping tables and recreating later. then you need to make sure tables are available before it executes above rather than using IF EXISTS------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Bjcascone
Starting Member
37 Posts |
Posted - 2010-08-12 : 16:11:11
|
| The tables may or may not be in the database depending on the week. We get some data on a weekly basis and the table gets created in the DB and then later merged with another table and dropped. other data is obtained monthly and taken through the same process. but evey week we need to run this proc to stratify the data in the table to make sure we have the correct data in the table. Author Topic BjcasconeStarting MemberUSA25 Posts Posted - 08/12/2010 : 14:57:46 -------------------------------------------------------------------------------- I am working on a Stored Proc that inserts some selected data from a set group of tables. the issue is that some times some of these tables do not exist. if the tables do not exist and the SP is run it will error out before completion. some of the script is hard coded. but here is what i have so far:Case when [JcpDataOraclePoLines].[dbo].[PoLinesAll_Load] exists then INSERT INTO Jcp..StratificationLoadTablesSelect 2608,-- AS AuditId,'POHeadersLines','LastUpdateDate','ExtAmt',Year(A.LastUpdateDate) as YY,Month(A.LastUpdateDate) as MM,Day(A.LastUpdateDate) as DD,Sum(CAST(ROUND(([UnitPrice] *[Quantity]), 2) as Money)) as Volume,Sum(1) as TotalFROM JcpDataOraclePO.dbo.POHeadersAll_load AJOIN [JcpDataOraclePoLines].[dbo].[PoLinesAll_Load] BON A.PoHeaderId = B.PoHEaderIdGroup By Year(A.LastUpdateDate), Month(A.LastUpdateDate),Day(A.LastUpdateDate)Order By Year(A.LastUpdateDate), Month(A.LastUpdateDate),Day(A.LastUpdateDate)ElseBasically i would like to preface this with an If Table exists clause but i am not sure how the syntax would go. any help or alternative ideas would be helpfulThanks tkizer I am going to try this method I think it may work. I will post back if i run into any issues. Thanks for the help. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2010-08-12 : 16:34:45
|
| [code]if object_id('dbo.MyTable','U') is not null begin < execute dynamic SQL to insert data here > end[/code]CODO ERGO SUM |
 |
|
|
Kokkula
Starting Member
41 Posts |
Posted - 2010-08-13 : 01:11:04
|
| Hello,Code should be like below.IF EXISTS (SELECT 1 FROM [JcpDataOraclePoLines].INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'PoLinesAll_Load'BEGIN //Your insert statement hereENDELSEBEGIN //Your else part to be hereENDHope its helpful....PavanInfosys Technologies Limited |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|