Calling SQLServer stored procedure from Sybase - sql

I'm writing a script in Sybase ASE where I call a stored procedure in SQL Server. The problem I have is that I'm getting an error from Sybase but not from SQL Server.
The Script in Sybase is as follows:
declare #input varchar(4000)
select #input = '111,222,333,444,555'
exec GATEWAY.MyDataBase..MyStoredProcedure #input
And this is the error:
[Error] Script lines: 1-5 --------------------------
Conversion failed when converting the varchar value '555.............................................................................................................................................' to data type int.
Msg: 245, Level: 16, State: 1
Server: GATEWAY, Line: 0
My stored procedure in SQLServer
I don't know why Sybase adds that right padding. The only way I can get it work is declaring the variable #input as varchar(255) or shorter.
What I tried without luck:
Adding a rtrim and ltrim in sybase
Setting the variable as TEXT
Adding rtrim and ltrim in SQLServer
adding a replace(#input,' ', '')
Wanted to replicate the problem in SQLServer adding the pad to the right but it still works.
The important thing is that: I can excecute it from SQLServer but it throws error from Sybase with the same input data.
Any help would be very appreciated. I guess it has something to do with the size of the varchar but no clue yet.

You can create a remote procedure in Sybase like this:
CREATE PROCEDURE "DBA"."uspSQL_CreateTicketsByCaseNo;1"( in #caseNo varchar(8) )
at 'SQL;dbName;dbo;uspSQL_CreateTicketsByCaseNo'
You can also right-click the Procedures & Functions and use the wizard to create a remote procedure. Then call the remote procedure like this:
CALL "DBA"."uspSQL_CreateTicketsByCaseNo;1"( caseNo )
It works for me.

Related

Syntax issue on the source

I'm migrating a SQL Server 2008 database to SQL Server 2019, I have used Microsoft Data Migration Assistant, to look for search any breaking changes, issues or syntax errors.
I getting errors for some of my procedures:
Object [dbo].[PROCEDURE1] has syntax errors. Must declare the variable or parameter "#SINI". Error number 70590. For more details, please see: Line 9, Column 16.
This is my procedure:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[PROCEDURE1]
#Refer AS varchar,
#Ret Decimal OUTPUT
AS
DECLARE #SIni AS Decimal
SET #SIni= (SELECT Ini FROM Table1 WHERE Refer = #Refer)
SET #Ret = #SINI
Probably you have a server with case sensitive collation. As is explained in the documentation, the identifiers for variables, GOTO labels, temporary stored procedures, and temporary tables are in the default collation of the server instance.. You may check this with the following simple statement:
SELECT SERVERPROPERTY('collation');
But, to fix the error, use the correct case-sensitive variable name:
...
SET #Ret = #SIni
...
As an additional note, declare your data types with the appropriate length (as #Larnu commented). The length attribute is optional, and in case of parameter declaration the SQL Server assigns 1 as length, so the #Refer parameter has data type varchar(1).

Table-valued type as parameter

I'm trying to create the following stored proc with table-valued type as one of the parameter. However, I'm getting error as
Incorrect syntax near 'READONLY'.
I've checked many stackoverflow responses which mentions that the reasons could be sql server version. This is what my ##version tells me and looks fine to me
Microsoft SQL Server 2014 (RTM-CU14) (KB3158271)
Create PROCEDURE [dbo].[Customer_Select](
#variableTable dbo.CustomVariableType READONLY,
#return BIT = NULL OUTPUT)
AS
BEGIN
SELECT * FROM Customer
SET #return=1
END
Appreciate your help in this regard
It started to work after I restarted SSMS. Weird!!

DB2 can't create a simple stored procedure

My real stored procedure is much more complex, but DB2 can't even seem to create a dummy stored procedure that does absolutely nothing (that's why I'm posting this dummy procedure). The procedure is as follows
CREATE PROCEDURE SIMPLE_DECL_PROC()
LANGUAGE SQL
BEGIN
DECLARE v varchar(16);//the problem is here
END
I'm getting the following error:
2:59:31 [CREATE - 0 row(s), 0.000 secs] [Error Code: -104, SQL State: 42601]
DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=END-OF-STATEMENT;ECLARE v varchar(16);<psm_semicolon>, DRIVER=3.57.82
The error complains about the declare statement although I can't see anything wrong with it (tried removing the ; at the end but it didn't work).
If I remove the declare statement, the procedure is created successfully.
I tried to do this with DBVisualizer as well as SQL Squirrel.
I'm sure it's something simple that I'm missing but I can't find it. Needless to say, I've checked similar questions here on SO and elsewhere.
And finally, we're using DB2 9.7.
Thanks
Most SQL clients allow to set a so-called statement terminator. This is the character that is at the end of the entire statement. By default most clients use the semicolon (";"). For stored procedures often a different statement terminator needs to be chosen and set. This is because a stored procedure can include multiple SQL statements which are terminated, but only that ouf the entire stored procedure is relevant.

SQL reporting invalid syntax when run in Power BI

I have written an SQL script which runs fine when executed directly in SQL Management Studio. However, when entering it into Power BI as a source, it reports that it has an incorrect syntax.
This is the query:
EXEC "dbo"."p_get_bank_balance" '2'
However, the syntax is apparently incorrect? See Picture:
Any help is much appreciated.
EDIT ***
When the double quotes are removed (as per Tab Alleman's suggestion):
I found time ago the same problem online on power bi site:
http://community.powerbi.com/t5/Desktop/Use-SQL-Store-Procedure-in-Power-BI/td-p/20269
You must be using DirectQuery mode, in which you cannot connect to data with stored procedures. Try again using Import mode or just use a SELECT statement directly.
In DirectQuery mode, PowerBI automatically wraps your query like so: select * from ( [your query] ), and if you attempt this in SSMS with a stored procedure i.e.
select * from (exec dbo.getData)
You get the error you see above.
The solution is you have to place your stored procedure call in an OPENQUERY call to your local server i.e.
select * from OPENQUERY(localServer, 'DatabaseName.dbo.getData')
Prerequisites would be: enabling local server access in OPENQUERY with
exec sp_serveroption #server = 'YourServerName'
,#optname = 'DATA ACCESS'
,#optvalue = 'TRUE'
And then making sure you use three-part notation in the OPENQUERY as all calls there default to the master database
With "Import" data connectivity mode Stored Procedures work
With "Direct Query" data connectivity mode, the query syntax must be like below:
declare #sqlCommand varchar(100) = 'dbo.p_get_bank_balance'
declare #p1 int = 2
exec #sqlCommand #p1 = #p1
Remerber: max one data source connection with Direct Query. If you want to call much SP, only one can be in Direct Query mode, the others in Import mode
Try using Import instead of Direct Query. It may be showing error cause you are using Temp table in it. Create query using sub query and remove Temp table and try it. Or you can use as Import instead of Direct Query it will work.

Conflict between Delphi and SQL server

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;
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?
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.
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.