如何检查一列在sqlserver中的任何表中使用 [英] how to check one column is used in any tables in sqlserver

查看:175
本文介绍了如何检查一列在sqlserver中的任何表中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有三个表Role,Master,Employee.角色具有两列Roleid和RoleName.

我在Master和Emplyee中使用了RoleId.我想从角色中删除单行".当我删除时,我想检查其他两个表中是否有删除行RoleID的记录.

我正在asp.net中做我的项目

Hi,

I have three tables Role,Master,Employee. Role have two columns Roleid and RoleName.

i used RoleId in Master and Emplyee. i want to delete One Row from Role. when i deletinb i want to check whether there is any record in other two tables with that deleting rows RoleID.

i am doing my project in asp.net

how can i do that?

推荐答案



引用此
Hi,

Refer this Link[^]


通常,我会说使用级联删除设置您的外键,例如 SQL Server中的CASCADE(带有示例) [
我再次考虑了一下,如果在上述情况下您的角色表不是父"表,但是如果要删除角色,则要更改其他表,则可以在要触发的角色表上创建触发器. c0>-可以在此处找到示例
Triggers-SQL Server [
Normally I would say set up your foreign keys with a cascade delete e.g. CASCADE in SQL Server with example[^] but beware which table you make the "parent" and which tables are "child" tables - I doubt you want to delete an Employee just because the Role disappears - you might want to reallocate them!

I thought about this again, and if your Role table is not the "parent" in the above context, but you want to change the other tables if a role is deleted, then you can create a Trigger on the Role table to be fired AFTER DELETE - an example can be found here http://www.tech-recipes.com/rx/41468/sql-server-coding-the-after-delete-trigger-in-sql-server/[^] and on CodeProject here - Triggers -- SQL Server[^]


这篇关于如何检查一列在sqlserver中的任何表中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆