0

Background:

I am using SMO to read a script and then execute it on my database. There are parts of my script that need to change before I execute it, like the data of the databases.

The variable is cleansed and checked before it is allowed, and can only contain upper case letters.

The variable is this case is prefix.

However for some reason when I use a variable in this particular part of the script I get the below error.

enter image description here

Code:

The line of code that produces this error report is shown below.

sql += @"FROM dbo." + prefix + "reportEmails";

The full block of code can be seen below.

     sql += @"

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO";
            sql += Environment.NewLine;
            sql += @"CREATE VIEW [dbo].[" + prefix + "reportEmails]";
            sql += @"AS
SELECT     EmailID, EmailContent, EmailSubject, EmailTo, EmailFrom, UserID, ObjectValueID, EmailSent, EmailCreated, EmailRead, EmailFromName, EmailType, EmailFailed, 
                      CASE WHEN emailread IS NULL THEN 'Not Read' ELSE 'Read' END AS EmailStatus
FROM         DEReportingClient2DB.dbo.Emails AS Emails_1
WHERE     (UserID IN
                          (SELECT     UserID
                            FROM          DEReportingClient2DB.dbo.Users
                            WHERE      (ClientID = 195)))
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO";
            sql += Environment.NewLine;
            sql += @"CREATE VIEW [dbo].[" + prefix + "unreadEmails]";
            sql += @"AS
SELECT     COUNT(*) AS UnreadEmails, UserID";
            sql += @"FROM dbo." + prefix + "reportEmails";
sql += @"WHERE     (EmailRead IS NULL)
GROUP BY UserID
GO
...

Question:

Why am I getting this error?

Incorrect syntax near '.'

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dan Cundy
  • 2,649
  • 2
  • 38
  • 65
  • 3
    You're missing required white space between elements when the string is concatenated. Look at what the SQL is that you are sending to be executed. – Martin Smith Mar 08 '15 at 13:49
  • 2
    Please show the _full_ code block you use to declare and initialize the C# variable `sql`. Also include the value of `sql` afterwards. – CodeCaster Mar 08 '15 at 13:49
  • the fourth line ' Go "; '. try remove the " – cyan Mar 08 '15 at 13:51

2 Answers2

1

Answer

After displaying what was actually being put into the variable SQL I discovered there were times where the code wasn't being put on a newline. This was causing joining of words.

String in SQL Variable

For Example, see line beginning with Select

enter image description here

Actual Solution

Adding a newline before the keywords.

sql+=Environment.NewLine;

Example

            sql += Environment.NewLine;
            sql += @"CREATE VIEW [dbo].[" + prefix + "unreadEmails]";
            sql += Environment.NewLine;
            sql += @"AS";
            sql += Environment.NewLine;
            sql += @"SELECT     COUNT(*) AS UnreadEmails, UserID";
            sql += Environment.NewLine;
            sql += @"FROM dbo." + prefix + "reportEmails";
            sql += Environment.NewLine;
Dan Cundy
  • 2,649
  • 2
  • 38
  • 65
0

"GO" is not actually SQL. It is recognized by sqlcmd.exe and SQL Server Mngmnt Studio as a batch delimiter. But it is not valid in SMO. You will have to remove all 'GO's and execute the batches in separate statements.

I guess I was wrong about GO and SMO. Thanks Martin.

Crowcoder
  • 11,250
  • 3
  • 36
  • 45
  • 1
    I may be wrong, but I was under the impression that it was valid in SMO. See accepted answer in this question. [how-do-i-execute-a-large-sql-script-with-go-commands](http://stackoverflow.com/questions/40814/how-do-i-execute-a-large-sql-script-with-go-commands-from-c) – Dan Cundy Mar 08 '15 at 14:58
  • @Dan Cundy if you read the answers they all talk about splitting the string on GO or replacing it somehow as a way to get around the fact that it contains GO. If you have control of the string it would be silly to put GOs in and then parse them out. But, you can always just try it and see what happens. – Crowcoder Mar 08 '15 at 15:05
  • 1
    [SMO understands "Go"](http://weblogs.asp.net/jongalloway/Handling-_2200_GO_2200_-Separators-in-SQL-Scripts-_2D00_-the-easy-way) – Martin Smith Mar 08 '15 at 19:02
  • I thought so, was the main reason I have used SMO. – Dan Cundy Mar 09 '15 at 11:14