| Author |
Topic |
|
Sun Foster
Aged Yak Warrior
515 Posts |
Posted - 2006-10-14 : 05:15:06
|
| I added a job to get new data every day using statement below:truncate table Ainsert into A select * from BHow to check table B condition before truncate A?For example, if B is not available or there is no data in it, stop truncate. |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-10-14 : 05:53:16
|
There will be 2 conditions If (Exists (Select * From Information_Schema.Tables where Table_Name = 'B')) And (Select Count(1) From B) >0 Truncate Table else --Do something differnt. Chiraghttp://chirikworld.blogspot.com/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-10-14 : 07:09:06
|
| Why do you want to have two tables with same data?MadhivananFailing to plan is Planning to fail |
 |
|
|
Sun Foster
Aged Yak Warrior
515 Posts |
Posted - 2006-10-14 : 07:36:40
|
| I tested it, it works great!Can I only use "Select Count(1) From B) >0" |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-10-14 : 07:41:48
|
| The first check, is too check that whether the table exists in the database. if you are 100% sure that table is there in the database then you remove that check only you use the second conditionChiraghttp://chirikworld.blogspot.com/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-10-14 : 07:59:16
|
I think you may need to nest these - in case SQL does the COUNT(*) before the TABLE EXISTSI know it should be left to right, but I've had trouble withIF IsDate(@MyDate) AND CONVERT(datetime, @MyDate) > @MyStartDatein the past ... can't remember exactly what the issues was ... but the solution would be:If (Exists (Select * From Information_Schema.Tables where Table_Name = 'B'))BEGIN IF (Select Count(1) From B) >0 BEGIN Truncate Table ENDENDelse --Do something different Kristen |
 |
|
|
Sun Foster
Aged Yak Warrior
515 Posts |
Posted - 2006-10-14 : 08:03:08
|
| I tested and found that if only use select count statement, in case of there is no table B, query will got error and stop.I have 20 tables need to truncate and insert, how to make job keep on going even one of tables not existing only using count statement? |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-10-14 : 08:13:55
|
or you can make use of this query if (SELECT i.rowcntFROM sysobjects oLEFT OUTER JOIN sysindexes iON o.[id] = i.[id]WHERE o.xtype = 'U' AND i.indid < 2 and o.[name] = 'TableName') > 0 --Trucate else - Dont Truncate. Chiraghttp://chirikworld.blogspot.com/ |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
|
|
Sun Foster
Aged Yak Warrior
515 Posts |
Posted - 2006-10-14 : 08:34:13
|
| Another question:If table B is in remote server, this statement did not work.How to make it work? If linked server already being created. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-10-14 : 08:58:39
|
| "only using count statement?"Why only use a COUNT statement? What problem do you have combining that with an EXISTS for the table itself?Kristen |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-10-14 : 09:07:27
|
| [code]if (SELECT i.rowcntFROM SERVERNAME.DATABASENAME.DBO.sysobjects oLEFT OUTER JOIN SERVERNAME.DATABASENAME.DBO.sysindexes iON o.[id] = i.[id]WHERE o.xtype = 'U' AND i.indid < 2 and o.[name] = 'TableName') > 0 --Trucate else - Dont Truncate.[/code]Chiraghttp://chirikworld.blogspot.com/ |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-10-15 : 00:42:03
|
You can check if the table exists with a function call.-- Check if table B existsif objectproperty(object_id('dbo.B'),'IsUserTable') = 1 begin -- Check if there are any rows in table B if (select top 1 1 from dbo.B ) > 0 begin truncate table dbo.A insert into dbo.A select * from dbo.B end endCODO ERGO SUM |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-10-15 : 13:57:07
|
| "if (select top 1 1 from dbo.B ) > 0"That's an interesting ideaI wonder how it fares compared toIF EXISTS (SELECT * FROM dbo.B )Kristen |
 |
|
|
|