Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating SQL query with line breaks in a single string

Tags:

c#

sql

ssms

In the SQL Server Management Studio I create a new query and then I run the following code:

select dbName.dbo.ScalarFunction()
union
select dbName2.dbo.ScalarFunction()

Then I try to do the same thing from my C# program. However I am having trouble to translate the above to one Query string. I have tried the following, but without any success:

string QueryString = @"select dbName.dbo.ScalarFunction() /r/n union /r/n select dbName2.dbo.ScalarFunction()"

and

string QueryString = @"select dbName.dbo.ScalarFunction(); union; select dbName2.dbo.ScalarFunction();"

and

string QueryString = @"select dbName.dbo.ScalarFunction(); union select dbName2.dbo.ScalarFunction();"

I am very new to SQL so if anyone could help me with the correct syntax I would be very happy! Thanks in advance!

like image 608
Robin Avatar asked Jul 01 '26 03:07

Robin


1 Answers

If you're using a verbatim string (i.e. defined with the @ sign before the opening quotes) then you can just press the return key in the middle of the definition, for example:

string QueryString = @"select dbName.dbo.ScalarFunction()
union
select dbName2.dbo.ScalarFunction()";
like image 138
Jon G Avatar answered Jul 02 '26 16:07

Jon G