1

How can I use custom function in sql query to set value of some variable.

My function is:

function myFunc(par){return par;}

My SQL query is:

 const string query =
            @"
            DECLARE @result varchar(100)
            SET @result= myFunc(N'test')

            INSERT[dbo].[TempTable] ([Name]) 
            VALUES (@result)
            ";
3
  • What version of SQL are you using? Commented Apr 17, 2016 at 9:16
  • 2014 and btw this code is written in Migration, not in Configuration.cs class Commented Apr 17, 2016 at 9:18
  • In SQL Server, you need to specify the schema name when calling the function . . . dbo.myfunc(N'test'). Commented Apr 17, 2016 at 11:11

1 Answer 1

1

You just need to specify the schema that this function belongs to. Assuming it is in schema dbo, just change this line:

SET @result= myFunc(N'test')

to

SET @result= dbo.myFunc(N'test')

If you like, you can half the size of the overall query by inserting directly from the function output - no @variable required:

INSERT INTO dbo.TempTable (Name) 
SELECT dbo.myFunc(N'test')
Sign up to request clarification or add additional context in comments.

2 Comments

I do that and get the following error: Cannot find either column "myFunc" or the user-defined function or aggregate "myFunc", or the name is ambiguous.
I have updated my answer; I'd answered on-the-fly and misunderstood what the problem was. @GordonLinoff was correct in his comment.

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.