Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a SQL function from C#. The code for creating the function works fine in SQL but as soon as I try do it in C# it fails. I've narrowed it down to being the way C# formats the CommandText into a single line.

This is my code

#region Function Code
            string functionCode =String.Format(@"USE [{0}]
                                    SET ANSI_NULLS ON
                                    GO
                                    SET QUOTED_IDENTIFIER ON
                                    GO

                                    CREATE FUNCTION fn_KeyValue
                                    (
	                                    @tableName varchar(255),
	                                    @columnName varchar(255)
                                    )
                                    RETURNS varchar(100)
                                    AS
                                    BEGIN
	                                    DECLARE @result varchar(100)

	                                    SELECT @result = constraint_type
	                                    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS a, INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE b
	                                    WHERE a.table_name= @tableName
	                                    AND a.constraint_name=b.constraint_name
	                                    AND b.column_name = @columnName

	                                    RETURN @result

                                    END
                                    GO", Settings.Instance);
            #endregion

            new SqlCommand()
            {
                CommandText = functionCode,
                CommandType = CommandType.Text,
                Connection = Settings.Connection
            }.ExecuteNonQuery();


The error I get is:

Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
'CREATE FUNCTION' must be the first statement in a query batch.
Must declare the scalar variable "@tableName".
A RETURN statement with a return value cannot be used in this context.
Incorrect syntax near 'GO'


Is there a way for me to format my string so that SQL can understand it??
Posted

I believe you can set quoted_identifiers and ansi_nulls inside the stored proc, so move those in there. You don't techniqally need the USE statement, so comment it out. Then, try your code again.
 
Share this answer
 
Comments
DominicZA 25-Apr-11 14:51pm    
I dont see how moving the quoted_identifiers and ansi_nulls will help my problem?? Also, I need the USE statement because I am connected to the master database.
Wayne Stewart_ 25-Apr-11 14:53pm    
Also, I don't believe you can use GO in a command string as it is not part of T-SQL. When you use them in management studio, they are not actually sent to the server. They are hints to send the current query batch to the server. Doing what John said should help, and get rid of the final GO. Everything should be fine.
Try adding \r\n"+ at the end of each line in the string. Also get rid of the @ symbol before the first quote.
 
Share this answer
 
Comments
DominicZA 25-Apr-11 14:08pm    
By adding the @ symbol at the beginning it adds the \r\n for new lines so this is fine!
Try this
C#
            string functionCode =String.Format(@"
USE [{0}]
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
EXEC('CREATE FUNCTION fn_KeyValue
(
   @tableName varchar(255),
   @columnName varchar(255)
   )
RETURNS varchar(100)
AS
BEGIN
   DECLARE @result varchar(100)

   SELECT @result = constraint_type
   FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS a,
        INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE b
   WHERE a.table_name= @tableName
         AND a.constraint_name=b.constraint_name
	 AND b.column_name = @columnName
 
   RETURN @result
 
END')", Settings.Instance);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900