简体   繁体   English

SQL Server xml和nvarchar列

[英]SQL Server xml and nvarchar columns

I wanted to achieve this by the query below , I have assigned the table names to the @table_names variable and want to now check if these tables exist in the database . 我想通过下面的查询来实现这一点,我已经将表名分配给@table_names变量,现在想检查数据库中是否存在这些表。 If they don't then I want to print their names and raise an error . 如果他们没有,我想打印他们的名字并报错。

BUT I think the IF OBJECT_ID(@TABLE_NAMES, 'U') IS NULL is failing and the print command is listing all the tables names regardless of their existence . 但是我认为IF OBJECT_ID(@TABLE_NAMES, 'U') IS NULL失败了,并且print命令列出了所有表名,而不管它们是否存在。

DECLARE @TABLE_NAMES nvarchar(MAX) =
(
        select distinct B.POP_TABLE_name + ' '
        from GEOLEVELS a
        left outer join GEOG b
        on a.GEOGid=b.GEOGid
        where   b.POP_TABLE_name is not null and 
                (a.x_COLUMN is not null and a.y_COLUMN is not null) OR
                a.z_column is not null 
                FOR XML PATH('') 

)
IF  OBJECT_ID(@TABLE_NAMES, 'U') IS NULL
PRINT 'Table not found : ' +  @TABLE_NAMES
RAISERROR('TABLE NOT FOUND %S',16,1,@TABLE_NAMES)

Your code is creating a local variable @TABLE_NAMES, then checking for a USER_TABLE (ie 'U') with that name. 您的代码正在创建一个本地变量@TABLE_NAMES,然后检查具有该名称的USER_TABLE(即“ U”)。

Since you are never creating a user table with the name @TABLE_NAMES, your OBJECT_ID(@TABLE_NAMES, 'U') will always be NULL. 由于您永远不会创建名称为@TABLE_NAMES的用户表,因此您的OBJECT_ID(@TABLE_NAMES,'U')将始终为NULL。

I think what you want is something like this: 我认为您想要的是这样的:

DECLARE @TABLE_NAMES (ID INT IDENTITY(1,1), Name VARCHAR(255))
INSERT INTO @TABLE_NAMES (Name)
select distinct B.POP_TABLE_name
from GEOLEVELS a
left outer join GEOG b
on a.GEOGid=b.GEOGid
where   b.POP_TABLE_name is not null and 
        (a.x_COLUMN is not null and a.y_COLUMN is not null) OR
        a.z_column is not null 


DECLARE @Counter INT = (SELECT COUNT(*) FROM @TABLE_NAMES)
DECLARE @CurrentName VARCHAR(255)
DECLARE @TablesNotFound TABLE (TableName VARCHAR(255))
WHILE @Counter > 0
    BEGIN
        SET @CurrentName = (SELECT Name FROM @TABLE_NAMES WHERE ID = @Counter)
        IF OBJECT_ID(@CurrentName) IS NULL
            BEGIN
                PRINT 'Table not found: ' + @CurrentName
                INSERT INTO @TablesNotFound (TableName)
                VALUES (@CurrentName)
                SET @Counter = @Counter - 1
            END
        ELSE 
            BEGIN
                SET @Counter = @Counter - 1
            END
    END

IF (SELECT COUNT(*) FROM @TablesNotFound) > 0
    BEGIN
        DECLARE @ErrorTables VARCHAR(MAX) = (SELECT STUFF((SELECT ',' + TableName FROM @TablesNotFound ORDER BY TableName FOR XML PATH ('')), 1, 1, ''))
        RAISERROR(@ErrorTables,16,1)
    END

This will put all of the table names matching your criteria into a table variable. 这会将与您的条件匹配的所有表名放入一个表变量中。 Then, it will iterate over the table (using the ID column and a counter), put the non-existent tables into an error table, then stuff the results into one text variable and push that out with the RAISERROR...it will also accomplish the printing of each table that fails. 然后,它将遍历表(使用ID列和计数器),将不存在的表放入错误表中,然后将结果填充到一个文本变量中,并使用RAISERROR将其推出...这还将完成每个失败表的打印。 The issue you are experiencing is that all your table names are being mashed together (though separated by a space) and "SQL" sees this as just one string of text; 您遇到的问题是所有表名都被混在一起(尽管之间用空格隔开),并且“ SQL”将其视为一个字符串。 there is nothing telling it how to distinguish the table names within the string from one another. 没有什么可以告诉它如何区分字符串中的表名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM