0

I want to define a function to get "order number" parameter and return "last name" of the customer.these two parameters are in different tables i am using inner join.

the errors i am getting are: Incorrect syntax near (.

Incorrect syntax near the keyword RETURN.

Must declare the scalar variable @FindingLastName.

my code:

com.CommandText = "Create function Sales.FindingLastName (@OrderNumber varchar(10)) Returns nvarchar(50) As Begin Declare @FindingLastName(Select LastName from Sales.OrderDetails INNER JOIN Sales.Customers RETURN @FindingLastName END GO)";
com.Connection = con;
MessageBox.Show(com.ExecuteNonQuery().ToString());
5
  • 1
    Why are you creating functions via code? Why don't you use simple script? Commented Jan 3, 2014 at 11:53
  • 1
    This has absolutely nothing to do with C#. The errors you get are entirely because of invalid SQL. Commented Jan 3, 2014 at 11:54
  • i am asked to create the function via code @ Marko Commented Jan 3, 2014 at 11:55
  • you can see it in my code , i am doing the same , how do you think i can fix it?thx @ Taosique Commented Jan 3, 2014 at 11:56
  • Compare your function statement with the examples given in this link at the end : msdn.microsoft.com/en-us/library/ms186755.aspx, I would also suggest to first run your script in SQL server than use it it as commanText, if you have to prefer this way. Commented Jan 3, 2014 at 12:22

3 Answers 3

3

You have lot of things missed in your SQL Syntax. Have a look at below SQL Syntax Change the ColumnNameto actual columnnames in join statement. May be you want to modify the select condition accordingly by adding WHERE clause.

Create function Sales.FindingLastName 
(@OrderNumber varchar(10))
Returns nvarchar(50) As 
Begin 
Declare @FindingLastName varchar(50) = 
(Select LastName from Sales.OrderDetails O INNER JOIN Sales.Customers C ON C.ColumnName =O.ColumnName)
RETURN @FindingLastName 
END 

One more question You can run this only once. Once the function is created, you can't create same function again. So i doubt are you sure you want to do it this way via c#?? !!

For more details on User Defined function in SQL Refer THIS LINK

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

2 Comments

@ Naveen thanks man , i did every changes you said the only problem is that when i run it in C# i get -1 !! why is that?
@ Parisan Run the select statement you wrote in your code directly in DB and check what it returns.
1

What stands out there is the GO. Firstly, GO is not SQL. It only exists to tools like SSMS (etc), which use it to split a long script into separate commands. Inside an actual command, it is simply a syntax error. Consequently, GO can never appear inside nesting such as parenthesis, or a BEGIN / END block.

Basically, remove the GO.

However, it is pretty rare to use DDL inside ADO.NET, unless part of an automated schema migration tool etc.

Comments

0

I'm no hero with SQL, but i think this should work

    Create function Sales.FindingLastName (@OrderNumber varchar(10)) 
    RETURNS nvarchar(50) 
    As 
    RETURN
    (
      Select LastName 
      from Sales.OrderDetails INNER JOIN Sales.Customers 
    );

1 Comment

Dear Remco i am writing this code of yours in C# with this format com.CommandText = "Create function Sales.FindingLastName (@OrderNumber varchar(10) RETURNS nvarchar(50) As RETURN(Select LastName from Sales.OrderDetails INNER JOIN Sales.Customers )"; and i get the error of "incorrect syntax near RETURNS" how can i correct it?

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.