if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[BOOKS]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) --drop table [dbo].[BOOKS] GO
if exists (select * from sysobjects where type='u' and name=表) drop 表
请问,使用UNION关联2个表单,怎么连接? 如果存在就使用UNION连接 如: Use MyCar if exists (select * from sysobjects where type='u' and name='Table1') Select SiteFileCode, Sum(DotRate) As DotRate From MyDB.dbo.Table1 Group By SiteFileCode if exists (select * from sysobjects where type='u' and name='Table2') Union Select SiteFileCode, Sum(DotRate) As DotRate From MyDB.dbo.Table2 Group By SiteFileCode这样好像不行的,
动态sql Use MyCar declare @sql varchar(8000) set @sql='' if exists (select * from sysobjects where type='u' and name='Table1') set @sql='Select SiteFileCode, Sum(DotRate) As DotRate From MyDB.dbo.Table1 Group By SiteFileCode' if exists (select * from sysobjects where type='u' and name='Table2') if @sql='' then set @sql='Select SiteFileCode, Sum(DotRate) As DotRate From MyDB.dbo.Table2 Group By SiteFileCode' else set @sql=@sql+'Union Select SiteFileCode, Sum(DotRate) As DotRate From MyDB.dbo.Table2 Group By SiteFileCode'exec(@sql) 改成这样试试看
drop 表
如: Use MyCar
if exists (select * from sysobjects where type='u' and name='Table1')
Select SiteFileCode, Sum(DotRate) As DotRate From MyDB.dbo.Table1 Group By SiteFileCode
if exists (select * from sysobjects where type='u' and name='Table2')
Union Select SiteFileCode, Sum(DotRate) As DotRate From MyDB.dbo.Table2 Group By SiteFileCode这样好像不行的,
Use MyCar
declare @sql varchar(8000)
set @sql=''
if exists (select * from sysobjects where type='u' and name='Table1')
set @sql='Select SiteFileCode, Sum(DotRate) As DotRate From MyDB.dbo.Table1 Group By SiteFileCode'
if exists (select * from sysobjects where type='u' and name='Table2')
if @sql='' then
set @sql='Select SiteFileCode, Sum(DotRate) As DotRate From MyDB.dbo.Table2 Group By SiteFileCode'
else
set @sql=@sql+'Union Select SiteFileCode, Sum(DotRate) As DotRate From MyDB.dbo.Table2 Group By SiteFileCode'exec(@sql)
改成这样试试看