【问题标题】:T-SQL stored procedure add ALTER TABLE functionT-SQL 存储过程添加 ALTER TABLE 函数
【发布时间】: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


【解决方案1】:

我最终自己弄清楚了,这就是答案。

CREATE PROCEDURE spForeignKeys
AS
BEGIN 
    ALTER TABLE [child] ADD FOREIGN KEY  ([household_id]) REFERENCES [household] ([household_id])

    ALTER TABLE [child] ADD FOREIGN KEY  ([panel_id]) REFERENCES [panel] ([panel_id])

    ALTER TABLE [child] ADD FOREIGN KEY  ([childcharacteristics_id]) REFERENCES [childcharacteristics] ([childcharacteristics_id])
    ALTER TABLE [child] ADD FOREIGN KEY  ([anthropometric_id]) REFERENCES [anthropometric] (anthropometric_id)
    ALTER TABLE [child] ADD FOREIGN KEY  ([immunisation_id]) REFERENCES [birthimmunisation] ([immunisation_id])
    ALTER TABLE [child] ADD FOREIGN KEY  ([health_id]) REFERENCES [childhealth] ([health_id])
    ALTER TABLE [child] ADD FOREIGN KEY  ([timeuse_id]) REFERENCES [timeuse] ([timeuse_id])
    ALTER TABLE [child] ADD FOREIGN KEY  ([habits_id]) REFERENCES [habits] ([habits_id])
    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 [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])
    ALTER TABLE [household] ADD FOREIGN KEY  ([wealth_id]) REFERENCES [wealthindex] ([wealth_id])
END;
EXECUTE spForeignKeys;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 2018-10-03
    • 2013-05-28
    • 1970-01-01
    • 2012-04-28
    相关资源
    最近更新 更多