简体   繁体   English

SQL Server-根据提供的值验证表列的值

[英]SQL Server - validate table column values against with values provided

I have the following task that want to solve using SQL Server's query and/or stored procedure, and I would really appreciated if someone can give me some directions. 我有以下要使用SQL Server的查询和/或存储过程解决的任务,如果有人可以给我一些指导,我将不胜感激。

Basically we have a data warehouse based on SQL Server. 基本上,我们有一个基于SQL Server的数据仓库。 I want to validate columns in certain tables to ensure the values in these columns are valid. 我想验证某些表中的列,以确保这些列中的值有效。

Example as below: 示例如下:

Table1 ColumnsToValidate specifies the table/columns in which values need to be validated. Table1 ColumnsToValidate指定需要在其中验证值的表/列。 In the example I want to validate the Gender column of the Customer table, and the State column of the Address table. 在示例中,我要验证“ Customer表的“ Gender列和“ Address表的“ State列。 And the validationID is a foreign-key to a table holding all the valid values ( Table2 ). validationID是指向包含所有有效值的表( Table2 )的外键。

Table2 ValidationValues : this table holds all valid values for specific validation rules. Table2 ValidationValues :该表包含特定验证规则的所有有效值。 In the example, validation rule #1 ( ValidationID = 1 ) has two valid values, and validation rule #2 specified 3 valid values. 在该示例中,验证规则#1( ValidationID = 1 )具有两个有效值,而验证规则#2指定了3个有效值。

I'd like to (using SQL) dynamically create a query based on values in Table 1, which accordingly selects the Customer.Gender column and the Address.State column, so the values in these columns can be validated against the values in Table 2. 我想(使用SQL)根据表1中的值动态创建查询,该查询将相应地选择Customer.Gender列和Address.State列,因此可以对照表2中的值来验证这些列中的值。

Table1: ColumnsToValidate 表1: ColumnsToValidate

TableName  |   ColumnName   |  ValidationID
-----------+----------------+-----------------
Customer   |    Gender      |   1       
Address    |    State       |   2       

Table2: ValidationValues 表2: 验证值

ValidationID | Values  
-------------+----------------
1            | Male      
1            | Female       
2            | NY      
2            | WA        
2            | CA      

Table3: Customer 表3: 客户

CustomerID | Gender  
-----------+----------------
111        | Male      
112        | Female       
113        | Unknown      
114        | NULL    

Table4: Address 表4: 地址

AddressID  | State   
-----------+----------------
211        | AL      
212        | NY       
213        | WA      
214        | NULL        

EDIT: I could write this in a C# program, but the program will be slow. 编辑:我可以在C#程序中编写此代码,但是该程序会很慢。 I would think there could be a way in pure SQL (SQL Server) 我认为纯SQL(SQL Server)中可能有一种方法

Here is my solution, in two steps - 这是我的解决方案,分两个步骤-

1) To validate the values "as if" the target table and column are known, eg the following outer join query finds invalid values on the Customer.Gender column. 1)为了验证值“好像”知道目标表和列,例如,以下外部联接查询在Customer.Gender列上找到无效的值。

select * from Customer a
left join ValidationValues b
on a.Gender = b.values
and a.ValidationID = b.ValidationID 
where b.values is null

2) Use dynamic-SQL to generate above SQL script, using values from table ColumnsToValidate , substituting table-name 'Customer' and column-name 'Gender' with variables @tab and @col1 : 2)使用dynamic-SQL生成上述SQL脚本,使用表ColumnsToValidate中的值,用变量@tab和@ col1替换表名'Customer'和列名'Gender':

declare @tableCursor cursor,
        @tab varchar(100),
        @col1 varchar(100),
        @val_id varchar(20)

set @tableCursor = cursor for select * from ColumnsToValidate

open @tableCursor
fetch next from @tableCursor into @tab, @col1, @val_id
while(@@fetch_status = 0)
begin
    --dynamic sql
    declare @sql varchar(max)

    set @sql = 

    N'select * from '+ @tab +' a ' +
    N'left join ValidationValues b ' +
    N'on a.' + @col1 + ' = b.values ' +
    N'and a.' + @val_id + ' = b.ValidationID ' +
    N'where b.values is null'

    --print @sql
    exec @sql

    fetch next from @tableCursor into @tab, @col1, @val_id
end

close @tableCursor
deallocate @tableCursor

As being said, these are mock-up codes and they are not tested. 可以这么说,这些是模型代码,未经测试。 However I'd just to share the ideas to people having similar problems - the key to the solution is "Dynamic SQL". 但是,我只想将想法分享给遇到类似问题的人员-解决方案的关键是“动态SQL”。

暂无
暂无

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

相关问题 将同一列中的值与 SQL Server 中的其他列进行比较 - Compare the values within the same column against other columns in SQL Server 检查并比较sql server表中的列值 - Check and compare column values in sql server table SQL - 与同一列中的多个值进行比较? - SQL - Comparing against multiple values in the same column? 根据 sql server 中提供的输入编号将列值更新为分组序列号 - Update the Column values as grouped Sequence number based on input number provided in sql server 如何通过比较一个表中的值与另一个表中的值来填充SQL中的列 - How to populate a column in SQL by comparing values from one table against another sql, javax.persistence.criteria: 检查一个数据库表列,用逗号分隔的字符串值对照字符串列表 - sql, javax.persistence.criteria: check a database table column with comma separated string values against a list of strings 根据另一个表中与第一个表中的列名相关的值,更新一个表中的SQL Server表值 - Update SQL Server table values in one table based upon values in another table that relate to the column names in the first SQL Server表转换查询/将列名称与值组合 - SQL Server Table Transformation Query / Combine Column Names With Values 使用另一个表SQL Server中的值替换列中的NAs - Replacing NAs in column with values from another table SQL Server SQL SERVER为现有表的每一行插入新列值 - SQL SERVER Insert New Column Values for every row of existing table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM