我可以从表值函数返回外键列吗?
我想使用表值函数并从多个存储过程中调用它,而不是在所有存储过程中重复相同的查询,但为了与旧版 VB6/UltraGrid 应用程序兼容,我需要保留原始表中的外键引用。
在表值函数中,我可以指定一个PRIMARY KEY
,但是有没有办法指定“FOREIGN KEY”?我尝试像这样修改返回表,但出现语法错误:
ALTER TABLE @ReturnTable
WITH CHECK ADD CONSTRAINT [FK_ReturnTable_OtherTable]
FOREIGN KEY([OtherTableID])
REFERENCES [dbo].[OtherTable] ([OtherTableID])
I want to use a table valued function and call it from within multiple stored procedures rather than repeat the same query across all the stored procedures, but for compatibility with a legacy VB6/UltraGrid app I need to preserve the foreign key references from the original tables.
Within the table valued function I can specify a PRIMARY KEY
, but is there a way to specify a 'FOREIGN KEY'? I tried to modify the return table like this, but I get a syntax error:
ALTER TABLE @ReturnTable
WITH CHECK ADD CONSTRAINT [FK_ReturnTable_OtherTable]
FOREIGN KEY([OtherTableID])
REFERENCES [dbo].[OtherTable] ([OtherTableID])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,你不能。该函数返回一个仅在运行时存在的“虚拟”表,并且无法 FK 到这样一个虚构/瞬态的行集。在不知道您的模式以及您想要完成什么的情况下,我最好的猜测是尝试使用触发器来强制执行此操作。
no you can't. the function returns a "virtual" table that exists only at run time and there is no way to FK to such a imaginary/transient set of rows. Without knowing your schema and exactly what you are trying to accomplish, my best guess it to try to use a trigger to enforce this.
您的旧网格会通过视图看到 FK 关系吗?或者查询无法存储为视图?
Will your legacy grid see the FK relationships through a view? Or is the query unable to be stored as a view?