How to insert a line break in a SQL Query string

Hello guys,
I am using the execute query activity to execute a query. I need some parts of the query to be on a newline.
I have tried using Environment.newline, vbclrf, vbNewLine, CRLF but the execute query keeps returning “Incorrect syntax”.
But when I output it using Writeline and Message Box the query is fine and works perfectly on mssql

HI @Dee1 - Any purpose to make the query in new line? Bcz execute query is an Activity which executes the query whatever has been given into that.

Thanks,
AK

@Dee1
As @AnandKumar26 said, the activity executes sql. This means you need to use sql syntax. You can use char(10) for line feed and char(13) for carriage return. Use these as necessary instead of the vb.net syntax.

Hi @AnandKumar26,
I am trying to run this query

IF OBJECT_ID(N’tempdb…#temptable’, N’U’) IS NOT NULL DROP TABLE #temptable;
GO
IF OBJECT_ID(N’tempdb…#temptable2’, N’U’) IS NOT NULL DROP TABLE #temptable2;
GO
IF OBJECT_ID(N’tempdb…#temptable3’, N’U’) IS NOT NULL DROP TABLE #temptable3;
GO

I built the string in an Assign Activity and to enable it look like the query above
The query looks like this in the assigned activity
"IF OBJECT_ID(N’tempdb…#temptable’, N’U’) IS NOT NULL " + environment.newline +
"DROP TABLE #temptable; " + environment.newline +
“GO” + environment.newline +
“IF OBJECT_ID(N’tempdb…#temptable2’, N’U’) IS NOT NULL” + environment.newline + “DROP TABLE #temptable2; " + environment.newline +
“GO”
“IF OBJECT_ID(N’tempdb…#temptable3’, N’U’) IS NOT NULL”+ environment.newline +” DROP TABLE #temptable3; " + environment.newline +
“GO”
This is to ensure it runs succesfully because if “GO” is not on a new line it would throw an error

Hello @DanielMitchell Thank you. I tried using Sql syntax also and got errors.

Thank you for your help @AnandKumar26

Hi
Welcome to uipath community
Fine no worries
–keep this query in a text file (.txt file)

IF OBJECT_ID(N’tempdb…#temptable’, N’U’) IS NOT NULL DROP TABLE #temptable;
GO
IF OBJECT_ID(N’tempdb…#temptable2’, N’U’) IS NOT NULL DROP TABLE #temptable2;
GO
IF OBJECT_ID(N’tempdb…#temptable3’, N’U’) IS NOT NULL DROP TABLE #temptable3;
GO

–use a read text file activity and pass the file path as input and get the output with a variable of type string named out_text
–now mention that string variable out_text in query property of the activity

simple isnt’ it
hope this would help you
kindly try this and let know for any queries or clarification
Cheers @Dee1

@Dee1

This won’t ever work since Environment.NewLine is not SQL syntax. Try using “char(13) + char(10)” instead, which is a carriage return followed by a line feed. This is how most NewLines work.

Hello @Palaniyappan thank you for your help. I tried it and still got an error “Incorrect Syntax near Go” but the query runs successfully on mssql

Try put it all onto a single line and run it?

Hi @TimK I tried that already, “Go” has to be on a newline.

Thank you everyone. I fixed it by using a storedProcedure and executing the stored procedure.

2 Likes

hello Im having the same issue but i cant have it stored !

Any solution around it!

Hazem