SQL - 使用表填充数据

时间:2017-12-20 12:02:00

标签: sql sql-server database count

我正在使用SQL语句来构建一个记录计数为的临时表。它插入4个字段,一个描述符和3个整数计数。最后一个字段CountBYODEntityStagingTable实际上是我使用sp_addlinkedserver链接到的另一个数据库。有时,如果表尚未发布,则该表将不存在。

我可以在下面的语句中添加什么逻辑,在我尝试计算之前检查表是否存在,所以它不会产生错误,请?

   INSERT INTO #TempTable (
    EntityName, 
    CountMainTable, 
    CountEntityView, 
    CountEntityStagingTable, 
    CountBYODEntityStagingTable
)  VALUES
('CustTransEntity', (SELECT count(*) FROM CustTrans), (SELECT count(*) FROM CustTransEntity), (SELECT count(*) FROM CustTransEntityStaging), (SELECT count(*) FROM DEVBYOD.dbo.CustTransEntityStaging)),
('VendTransEntity', (SELECT count(*) FROM VendTrans), (SELECT count(*) FROM VendTransEntity), (SELECT count(*) FROM VendTransEntityStaging), (SELECT count(*) FROM DEVBYOD.dbo.VendTransEntityStaging)),
('TaxTransEntity', (SELECT count(*) FROM TaxTrans), (SELECT count(*) FROM TaxTransEntity), (SELECT count(*) FROM TaxTransEntityStaging), (SELECT count(*) FROM DEVBYOD.dbo.TaxTransEntityStaging))

非常感谢你能提供帮助

安德鲁

3 个答案:

答案 0 :(得分:0)

你可以这样做。请注意,我不知道您的Linkedserver是什么类型的数据库,但这适用于SQL数据库。

  

请记住在SQL变量中更改YOURDATABASENAME。

DECLARE @CustTransNo int = (select count(*) from CUSTRANS)
DECLARE @VendTransNo int =(select count(*) from VendTrans)
DECLARE @TaxTransNo int =(select count(*) from TaxTrans)

DECLARE @CustTransEntity int = (select count(*) from CustTransEntity)
DECLARE @VendTransEntity int =(select count(*) from VendTransEntity)
DECLARE @TaxTransEntity int =(select count(*) from TaxTransEntity)


DECLARE @CustTransEntityStaging int = (select count(*) from CustTransEntityStaging)
DECLARE @VendTransEntityStaging int =(select count(*) from VendTransEntityStaging)
DECLARE @TaxTransEntityStaging int =(select count(*) from TaxTransEntityStaging)


----Check if they exists


--CustTrans
DECLARE @CustTransEntityStagingBYOD int 
DECLARE @LinkedServerNameCust nvarchar(50) = 'MYLINKEDSERVERNAME'
DECLARE @SQLCustTrans NVARCHAR(MAX) = ''
DECLARE @TableExistsCustTrans BIT;

SET @SQLCustTrans = 'SELECT @TableExists = CASE WHEN TableExists = 0 THEN 0 ELSE 1 END
            FROM OPENQUERY(' + QUOTENAME(@LinkedServerNameCust) 
            + ', ''SELECT TableExists = COUNT(*) 
                    FROM YOURDATABASENAME.INFORMATION_SCHEMA.TABLES 
                    WHERE TABLE_NAME = ''''CustTransEntityStaging'''''');';

EXECUTE sp_executesql @SQLCustTrans, N'@TableExists BIT OUTPUT', @TableExistsCustTrans OUT;

IF(@TableExistsCustTrans = 1)
BEGIN
SET  @CustTransEntityStagingBYOD = (select count(*) from DEVBYOD.dbo.CustTransEntityStaging)
END
ELSE
BEGIN 
SET @CustTransEntityStagingBYOD  = 0
END

--VendTrans
DECLARE @VendTransEntityStagingBYOD int 
DECLARE @LinkedServerNameVend nvarchar(50) = 'MYLINKEDSERVERNAME'
DECLARE @SQLVendTrans NVARCHAR(MAX) = ''
DECLARE @TableExistsVendTrans BIT;

SET @SQLVendTrans = 'SELECT @TableExists = CASE WHEN TableExists = 0 THEN 0 ELSE 1 END
            FROM OPENQUERY(' + QUOTENAME(@LinkedServerNameVend) 
            + ', ''SELECT TableExists = COUNT(*) 
                    FROM YOURDATABASENAME.INFORMATION_SCHEMA.TABLES
                    WHERE TABLE_NAME = ''''VendTransEntityStaging'''''');';

EXECUTE sp_executesql @SQLVendTrans, N'@TableExists BIT OUTPUT', @TableExistsVendTrans OUT;

IF(@TableExistsVendTrans = 1)
BEGIN
SET  @VendTransEntityStagingBYOD = (select count(*) from DEVBYOD.dbo.VendTransEntityStaging)
END
ELSE
BEGIN 
SET @VendTransEntityStagingBYOD  = 0
END

--TaxTrans
DECLARE @TaxTransEntityStagingBYOD int 
DECLARE @LinkedServerNameTax nvarchar(50) = 'MYLINKEDSERVERNAME'
DECLARE @SQLTaxTrans NVARCHAR(MAX) = ''
DECLARE @TableExistsTaxTrans BIT;

SET @SQLTaxTrans = 'SELECT @TableExists = CASE WHEN TableExists = 0 THEN 0 ELSE 1 END
            FROM OPENQUERY(' + QUOTENAME(@LinkedServerNameTax) 
            + ', ''SELECT TableExists = COUNT(*) 
                    FROM YOURDATABASENAME.INFORMATION_SCHEMA.TABLES
                    WHERE TABLE_NAME = ''''TaxTransEntityStaging'''''');';

EXECUTE sp_executesql @SQLTaxTrans, N'@TableExists BIT OUTPUT', @TableExistsTaxTrans OUT;

IF(@TableExistsTaxTrans = 1)
BEGIN
SET  @TaxTransEntityStagingBYOD = (select count(*) from DEVBYOD.dbo.TaxTransEntityStaging)
END
ELSE
BEGIN 
SET @TaxTransEntityStagingBYOD  = 0
END

INSERT INTO #TempTable (EntityName, CountMainTable, CountEntityView, CountEntityStagingTable, CountBYODEntityStagingTable)  

VALUES
('CustTransEntity', @CustTransNo, @CustTransEntity, @CustTransEntityStaging, @CustTransEntityStagingBYOD),
('VendTransEntity',@VendTransNo, @VendTransEntity, @VendTransEntityStaging, @VendTransEntityStagingBYOD),
('TaxTransEntity', @TaxTransNo, @TaxTransEntity, @TaxTransEntityStaging, @TaxTransEntityStagingBYOD)

答案 1 :(得分:0)

我认为最简单的方法是为所有存在的表(创建表)维护一个主表。插入时只需检查主表。在这种情况下,你不会得到任何错误。

INSERT INTO #TempTable (EntityName, CountMainTable, CountEntityView, CountEntityStagingTable, CountBYODEntityStagingTable)  VALUES
('CustTransEntity', (SELECT count(*) FROM CustTrans), (SELECT count(*) FROM CustTransEntity), (SELECT count(*) FROM CustTransEntityStaging), (SELECT CASE WHEN CustTransEntityStaging = (SELECT * from TableMaster) THEN (select CAST(COUNT(*) as varchar) from DEVBYOD.dbo.CustTransEntityStaging) ELSE 'No such Table' END))

答案 2 :(得分:0)

这很简单!

 DECLARE @CustTransEntityStagingCount INT = 0 ,
         @VendTransEntityStagingCount INT = 0,
         @TaxTransEntityStagingCount INT = 0



 IF EXISTS (
        SELECT 1
        FROM   DEVBYOD.sys.Tables -- [LinkedServerName].[DatabaseName].sys.Tables
        WHERE  [Name] = 'CustTransEntityStaging'
    )
     SELECT @CustTransEntityStagingCount = COUNT(*)
     FROM   DEVBYOD.dbo.CustTransEntityStaging


     IF EXISTS (
        SELECT 1
        FROM   DEVBYOD.sys.Tables -- [LinkedServerName].[DatabaseName].sys.Tables
        WHERE  [Name] = 'VendTransEntityStaging'
    )
     SELECT @VendTransEntityStagingCount = COUNT(*)
     FROM   DEVBYOD.dbo.VendTransEntityStaging


     IF EXISTS (
        SELECT 1
        FROM   DEVBYOD.sys.Tables -- [LinkedServerName].[DatabaseName].sys.Tables
        WHERE  [Name] = 'VendTransEntityStaging'
    )
     SELECT @TaxTransEntityStagingCount = COUNT(*)
     FROM   DEVBYOD.dbo.TaxTransEntityStaging


INSERT INTO #TempTable
  (
    EntityName,
    CountMainTable,
    CountEntityView,
    CountEntityStagingTable,
    CountBYODEntityStagingTable
  )
VALUES
  (
    'CustTransEntity',
    (
        SELECT COUNT(*)
        FROM   CustTrans
    ),
    (
        SELECT COUNT(*)
        FROM   CustTransEntity
    ),
    (
        SELECT COUNT(*)
        FROM   CustTransEntityStaging
    ),
    (
        @CustTransEntityStagingCount
    )
  ),
(
    'VendTransEntity',
    (
        SELECT COUNT(*)
        FROM   VendTrans
    ),
    (
        SELECT COUNT(*)
        FROM   VendTransEntity
    ),
    (
        SELECT COUNT(*)
        FROM   VendTransEntityStaging
    ),
    (
        @VendTransEntityStagingCount
    )
),
(
    'TaxTransEntity',
    (
        SELECT COUNT(*)
        FROM   TaxTrans
    ),
    (
        SELECT COUNT(*)
        FROM   TaxTransEntity
    ),
    (
        SELECT COUNT(*)
        FROM   TaxTransEntityStaging
    ),
    (
        @TaxTransEntityStagingCount
    )
)