【发布时间】:2020-07-09 09:28:05
【问题描述】:
我使用 alter table 函数在表中添加了这些外键。
我想将它们添加为一个存储过程,因此当我执行存储过程时,它将运行并将这些外键添加到我的表中。有什么帮助吗?谢谢
ALTER TABLE [child]
ADD FOREIGN KEY ([mother_father_caregiver_id])
REFERENCES [education] ([education_id])
ALTER TABLE [child]
ADD FOREIGN KEY ([education_id])
REFERENCES [caregiver] ([mother_father_caregiver_id])
ALTER TABLE [child]
ADD FOREIGN KEY ([household_id])
REFERENCES [household] ([household_id])
ALTER TABLE [household]
ADD FOREIGN KEY ([householdage_id])
REFERENCES [householdage] ([householdage_id])
ALTER TABLE [household]
ADD FOREIGN KEY ([livestock_id])
REFERENCES [livestock] ([livestock_id])
ALTER TABLE [household]
ADD FOREIGN KEY ([credit_id])
REFERENCES [credit] ([credit_id])
ALTER TABLE [household]
ADD FOREIGN KEY ([shock_id])
REFERENCES [householdshocks] ([shock_id])
【问题讨论】:
-
这是一个语句而不是一个函数。为什么需要这样做?
-
这种方式运行良好,但我需要将其转换为存储过程。我试过了,它不起作用,我收到一个错误。
-
创建过程 [FOREIGN_KEYS] BEGIN ALTER TABLE [child] 添加外键 ([mother_father_caregiver_id]) REFERENCES [education] ([education_id]) ALTER TABLE [child] 添加外键 ([education_id]) REFERENCES [caregiver] ([mother_father_caregiver_id]) ALTER TABLE [child] 添加外键 ([household_id]) REFERENCES [household] ([household_id]) END
-
为什么要在 SP 中使用这些?一旦它们运行,SP 就毫无意义。
标签: sql-server tsql stored-procedures alter-table ssms-2017