Annoyance of the Day

time to read 3 min | 513 words

Can't figure out why it is not possible to pass a result set to a stored procedure. I want to bulid something like this:

CREATE PROCEDURE Ensure_FK

      @missing_fk table(fk int)

AS

      INSERT INTO Parent_Table (FK, Desc)

      SELECT fk, 'no description' FROM @missing_fk

      WHERE fk NOT IN (SELECT fk FROM Parent_Table)

Usage:

SELECT

FK INTO #Fks FROM Child_Table;
exec Ensure_FK #Fks

I dug around the documentation, and it looks like table variables can not be used as a procedure parameter :-(