1

I have a scenario where I have created a function which is returning a table variable. I want to use this table variable inside a insert statement. Below is the query

INSERT INTO DNVPCDCS..d_CMRequest      
    (PRIORITY, RETRIES,ACTIONCD,FILENAME,TAXONOMY,NOTES,GUID,TOKEN,STATUS,ISDBUSER)       
    VALUES      
    @PRIORITY,0,'INS',@FILENAME,[dbo].[udf_GetTaxonomy](@IMAGEID,@HISTORYNO,@NEW_GUID),@NOTES,@NEW_GUID,@TOKEN,'N',NULL

I am not sure about the syntax and am getting error on this.

4
  • That is so wrong I can't even see what you try to do. Commented May 18, 2016 at 10:24
  • One error is that there are no parenthesis around your Values Commented May 18, 2016 at 10:27
  • Why would you try to insert a table valued function call into a table cell? What does your function return? Commented May 18, 2016 at 10:29
  • The function is returning a table from which I need to select values and insert into another table. Commented May 18, 2016 at 11:36

2 Answers 2

2

You can use INSERT...SELECT syntax like this:

INSERT INTO DNVPCDCS..d_CMRequest(PRIORITY, RETRIES,ACTIONCD,FILENAME,TAXONOMY,NOTES,GUID,TOKEN,STATUS,ISDBUSER)
SELECT @PRIORITY,0,'INS',@FILENAME, ... 
    FROM [dbo].[udf_GetTaxonomy](@IMAGEID,@HISTORYNO,@NEW_GUID) t 

"..." replaces column lists because I have no idea what columns returned by udf_GetTaxonomy.

See INSERT (Transact-SQL) for more details.

Sign up to request clarification or add additional context in comments.

Comments

0

Replace VALUES with SELECT and the same will work. Your code will be:

INSERT INTO DNVPCDCS..d_CMRequest     
(PRIORITY, RETRIES, ACTIONCD, FILENAME, TAXONOMY, NOTES, [GUID], TOKEN, [STATUS], ISDBUSER)       
SELECT      
@PRIORITY, 0, 'INS', @FILENAME, [dbo].[udf_GetTaxonomy](@IMAGEID, @HISTORYNO, @NEW_GUID), @NOTES, @NEW_GUID, @TOKEN, 'N', NULL

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.