简体   繁体   English

验证SQL Server表中的数据

[英]Validate Data in SQL Server Table

I am trying to validate the data present in SQL Server table using a stored procedure. 我正在尝试使用存储过程来验证SQL Server表中存在的数据。 In one of the validation rules, i have to check whether the value of a particular column is present in another table. 在一个验证规则中,我必须检查另一个表中是否存在特定列的值。

Suppose i have a staging table with following columns Cat_ID, Amount, SRC_CDE 假设我有一个登台表,其中包含以下列Cat_ID,Amount,SRC_CDE

I have a 'maintable' with following columns CatID , Cat_Name 我有一个“ maintable”,其以下各列CatID,Cat_Name

I have to validate whether the Cat_ID present in staging table exists in the 'maintable' for each row 我必须验证临时表中存在的Cat_ID在每一行的“ maintable”中是否存在

I am using the following statement to validate 我正在使用以下语句进行验证

if((Select count(*) from maintable where CatID= @Cat_id) >0 )
-- Do something if data present

I want to know if there is any better way of doing the above thing other than using a select query for every row. 我想知道除了对每行使用选择查询之外,还有什么更好的方法可以做上述事情。

Can i use some sort of an array where i can fetch all the CatID from maintable and the check instead of using a select query. 我可以使用某种数组来从主表和支票中获取所有CatID而不是使用select查询吗?

Thanks 谢谢

Using a left join to list all the invalid rows. 使用left join列出所有无效行。

select
     staging.*
from
    staging
          left join maintable 
               on staging.catid=maintable.catid
where maintable.catid is null

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

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