1

I have a Query that works in SQL Server as well but when i save it in ado query in delphi it doesn't work and stops with this error :

Incorrect syntax near 'GO'

But the below code is correct and has not any error . i was tested it in sql server . The below code is not Regular because i copy and past it from delphi .

My Query :

create function GetTedad(@pfcode INT, @pdcode INT) returns int
as begin declare @Tedad int;
select @Tedad= sum(t2.tedade_avalie) from Tbl_avalie_salon t2 where t2.FCode = @pfcode and t2.DCode = @pdcode
return (@Tedad); end;
GO
create function getSumBSen2(@pfcode INT, @pdcode INT, @pSen INT) returns int
as begin declare @r int;
select @r= sum(t2.t_shab + t2.t_rooz) from tbl_talafat_dan t2 where t2.FCode = @pfcode and t2.DCode = @pdcode and t2.sen <= @pSen;
return (@r); end;
GO
select t1.sen, sum(t1.d_rooz) as d1, sum(t1.d_shab) as d2, sum(t1.d_rooz + t1.d_shab) as d_sum,
Round((sum((1000*(t1.d_rooz+t1.d_shab)+0.01)/((dbo.GetTedad(81, 1))-(dbo.getSumBSen2(81, 1, t1.sen))))),1) as Saraneh
from tbl_talafat_dan t1 where t1.FCode =81 and t1.DCode = 1 group by t1.sen;
user980115
  • 83
  • 1
  • 1
  • 5
  • 1
    Try search and replace all lines with only GO on the line with a semi-colon. I have a hunch this might work, but never taken the time to test it properly. – Disillusioned Oct 05 '11 at 12:05
  • @CraigYoung - That will not work. `create function` has to be in a batch of it's own. – Mikael Eriksson Oct 05 '11 at 12:32
  • @MikaelEriksson thanks, I didn't know whether semi-colon would be treated as a batch separator... I guess not. :( However, I have found a link to another option seriously worth considering: http://weblogs.asp.net/jgalloway/archive/2006/11/07/Handling-_2200_GO_2200_-Separators-in-SQL-Scripts-_2D00_-the-easy-way.aspx – Disillusioned Oct 06 '11 at 09:10

3 Answers3

5

The GO keyword is not a SQL Server statement

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.

You must remove this statement from your Delphi Code in order to execute your Sql sentence. check this question for an example How to run a database script file from Delphi?

Community
  • 1
  • 1
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • So how can i run my functions ,,,, if i dont use GO the code doesn't works at all . you mean i cant use 2 functions in one query ? – user980115 Oct 05 '11 at 12:24
  • That depends of the component which you are using. Check the sample posted in the question link. – RRUZ Oct 05 '11 at 12:35
1

You cannot do multiple statements in a Delphi query.
Put each block before each go in its own query and run them in sequence.

Then it should work.
Do not put the go statement in the Delphi query, it does go implicitly.

Johan
  • 74,508
  • 24
  • 191
  • 319
1

What you are executing is a script where each individual statement is separated with a GOstatement.

  • SSMS knows how to interprete these statements and execute them one at the time.
  • ADO does not know how to interprete these statements.

You could either

  • parse the statement yourself and execute each individual statement with a TADOQuery.
  • place each statement in a TADOQuery object of its own.

From GO(Transact-SQL)

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.

SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to an instance of SQL Server. The current batch of statements is composed of all statements entered since the last GO, or since the start of the ad hoc session or script if this is the first GO.

Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146