Skip to content
Advertisement

Inserting data into table from table-valued parameter

I am trying to create a stored procedure to which is passed a TVP and then some data from the TVP is inserted into two tables.

I have already implemented the stored procedure, but only the second insert (the only one that does not read from the TVP) is working. The other two are not working (do not insert anything) and I can’t seem to figure out why.

I have tried to create a dummy TVP in SQL Server and run the procedure there, but that also did not work. Is this being caused by the fact TVPs are readonly? I would assume not, since I am not actually inserting or updating data inside the TVP.

Is there a way to make this work?

Thank you for your assistance!

Table-valued parameter definition:

Stored procedure:

Advertisement

Answer

  1. Rather than using VALUES with sub-queries, just use SELECT.
  2. Always list the columns you are inserting into. Its clearer and will reduce errors especially if you modify the table structure in future,
  3. Your first query appeared to be overcomplicated – if indeed it worked at all.
  4. Your third query should have thrown an error because you have multiple IN sub-queries which should have resulted in a “sub-query returned multiple results” error.
  5. The text datatype is depreciated use varchar(max).
  6. Normally you want to SET NOCOUNT, XACT_ABORT ON.
  7. Always RETURN a status so your calling app knows whether it succeeded or not. 0 will be returned by default by I prefer to be explicit.
  8. Semi-colon terminate all statements.
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement