3

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!

7
  • I don't think you have to have a break line when you make union query. Commented Jul 10, 2013 at 15:11
  • "without any success": please state what happens. A runtime error message? Commented Jul 10, 2013 at 15:11
  • I did not recieve any error messages. However I only recieved the data from the first select and the rest was just ignored Commented Jul 10, 2013 at 15:12
  • Forget about the breakline. Do not include /r/n in your string. Commented Jul 10, 2013 at 15:14
  • .. and don't put in ";"s. What's inside the quotes should be what SQL expects, don't try to turn it into C#-like statements. Commented Jul 10, 2013 at 15:16

3 Answers 3

7

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()";
Sign up to request clarification or add additional context in comments.

Comments

5

i dont think you need to worry about the newline

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

1 Comment

This solution was the most obvious one from the beginning, but it didn't work in the SSMS, so I discarded it without testing it in my own program first. Silly me. Anyways, thanks for the help!
5

The carriage return should be \r\n and not /r/n

You can also use Environment.NewLine

Anyway, as stated in other answers, you should not worry about line breaks in SQL

2 Comments

Good job, spotting that mistake.
Fair enough. Still not the required result... :/

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.