Execute sybase stored procedure as linked server procedure sql server 2008 - sql

EDIT
The final goal is to call a stored procedure hosted in sybase with input and output parameters from SQL Server 2008 via Linked Server
I think title is pretty clear.
My goal is to execute a stored procedure hosted in Sybase SQL Anywhere 8 in SQL Server 2008 through the linked server I already created.
Any SQL query made through the linked server is working.
In addition I was able to execute a function but I don't now how to get the return value like that
EXEC ('CALL "dbname"."procedurename"(''param1'', ''param2'', ''param3'')') AT LinkedServerAlias;
Thanks 4 all your help!
Mauro

can you use four part naming convention?
like
exec LinkedServerName.dbname.dbo.procedurename #param1, #param2, #param3

I was finally able to do it by calling
SELECT * FROM OPENQUERY([LinkedServer], 'SELECT "dbname"."spname"(#p1,#p2, #p3)')
I'll add comments and example as soon as I experiment it.

4 part object names are valid only for SQL Server linked servers.
You have to have your EXEC inside an OPENQUERY
SELECT * FROM OPENQUERY([LinkedServer], 'EXEC MyDB.MyScheme.MyProc.spname #p1, #p2, #p3')
Now, you can't parametrise OPENQUERY calls so you have use dynamic SQL
DECLARE #sql nvarchar(4000), #linkedsql nvarchar(4000)
SET #sql = 'EXEC MyDB.MyScheme.MyProc.spname ' + CAST(#p1value as int) + ...
SET #linkedsql = 'SELECT * FROM OPENQUERY(LinkedServer, ''' + #sql + ''')'
EXEC (#linkedsql)

Related

Using openrowset in SQL Server 2014

I have the below SQL statement that I was using to get data from a stored procedure into a temp table
SET #sql = N'SELECT * INTO #CustomTable3HTML
FROM
OPENROWSET(''SQLNCLI'',''Server='+ ##SERVERNAME +';UID='+#UserName+';PWD='+#Password+';Database=SalesDeals'',
''SET NOCOUNT ON;SET FMTONLY OFF;EXEC dbo.prGetDealProposalDetail ''''' + #DealID + ''''',''''' + #FeeType +''''''')
'
EXEC sp_executesql #sql
This was working fine in SQL Server 2008, but after the server got upgraded to SQL Server 2014, I started getting this error:
The metadata could not be determined because statement 'Insert #TempDetail exec prExcelCalculations_GetPricingSummary #Dealno,'FSA'' in procedure 'prGetDealProposalDetail' uses a temp table.
Which is because in the stored procedure I call in the Openrowset calls another procedure and uses temp tables..The columns are dynamic.
Can someone please let me know what can I do to fix it?

SQL Server Linked Server Join

I have added a linked server in my sql server 2008. I want to fetch data from a table and a table valued function residing on my linked server and join this data on a local table by following below given naming convention.
<server>.<database>.<schema>.<table>
However my first problem is I need to fetch <server> part from a table. So when I try to do something like following it fails
Select * FROM #ServerNameVariable.database.dbo.myTable
Any idea how can I form fully qualified linked server table name with a user defined variable ?
My SP is as follows
CREATE PROCEDURE TEST_SP
AS
BEGIN
DECLARE #NetworkDBName VARCHAR(255)
SET #NetworkDBName = '[MyLinkedServerName]'
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
select * from #NetworkDBName + '.' + testDatabase.dbo.Invoice_tb
END
GO
You cannot use variables in place of database, schema or table names.
Instead you can build and execute dynamic SQL statements, using sp_ExecuteSQL.
This example won't work, as the server name is seen as a string and not a server object.
Failed Example
/* Anti-pattern.
* Does not work.
*/
DECLARE #Server SYSNAME = 'Server001';
SELECT
*
FROM
#Server.Database1.dbo.Table1
;
This example shows a method that does work. Here the SQL statement is built as a string, which is then executed.
/* Dynamic SQL statement.
* Will work.
*/
DECLARE #Server SYSNAME = 'Server001';
DECLARE #Statement NVARCHAR(255);
SET #Statement = 'SELECT * FROM ' + QUOTENAME(#Server) + '.Database1.dbo.Table1;';
EXECUTE sp_ExecuteSQL #Statement;
As ever; please be careful when generating and executing dynamic SQL statements. You do not want to open yourself up to SQL injection attacks. Look into OPENROWSET or check the passed server name against the code kindly supplied by #Devart above (SELECT name FROM sys.servers WHERE server_id > 0) before executing.
EDIT 1: Added more detail to the paragraph on SQL injection.
EDIT 2: Removed square brackets from 2nd example query, replaced with QUOTENAME, as per #TTs comment.
Use the name of link server it will a 4 part qualifier e.g
Select * From [Link Server Name ].[Database].[Schema].[Table]

sql server parameter size overloading

i am calling a stored procedure from my ASP.NET application. the stored procedure takes one parameter. the value that i am providing from my WEB Form is too large that it did not fully loading in variable of sql server. the data type of my sql server parameter is nvarchar(max) and the data type in my ASP.NET application is string.
the stored procedure is as below
Create procedure p_getProducts
#nm_emp nvarchar(max)
AS
BEGIN
select * from tblProduct where nm_user in(convert(nvarchar(max),#nm_emp));
END
please tell me which sql server data type i should use to overcome this problem.
Thanks.
For what I could suppose from your code, you should work with dynamic-sql and not using directly the parameter as value for the IN clause. Try with this proc.
Create procedure p_getProducts
#nm_emp nvarchar(max)
AS
BEGIN
DECLARE #SQL NVARCHAR(MAX);
SELECT #SQL = N'select * from tblProduct where nm_user in(' +
#nm_emp + N')'
EXEC sp_executeSQL #SQL

SQL: Executing a query to dynamic remote server

I want to be able to execute remote queries based on the results of a local query.
For instance:
DECLARE #REMOTESERVER VARCHAR(10)
Select TOP 1 #REMOTESERVER = RemoteServer from TABLE
--Execute the next query on a remote server from the value I retrieved above
Select * from tblCustomers
What RDBMS are you using? Some will not support a pure sql way of doing this. Others, like SQL Server, might support this scenario. Is the remote server accessible via a linked server that you can access. You could then use dynamic sql to create your sql string. Something like this should work in SQL Server:
SET #Sql = 'SELECT * FROM [' + #RemoteServer + '].dbname.schema.tblCustomers'
EXEC #Sql
Here is a post about linked servers: https://stackoverflow.com/a/4091984/1073631

Dynamic Linked Server Query

Is it possible to construct a dynamic query to for a linked server (and if so how)?
For example:
#linkedServer varchar(50)
#var1 varchar(10)
#var2 varchar(10)
select *
from openquery(#linkedServer,
'select c1,c2
from t1
where p1 = #var1
and p2= #var2')
example
exec ('select * from openquery(' + #linkedServer +
', ''select c1,c2 from t1 where p1 = '' + #var1 + ''and p2= '' + #var2 + ''')
make sure to read The Curse and Blessings of Dynamic SQL to protect against SQL injection
see EXEC() at Linked Server section of The Curse and Blessings of Dynamic SQL by Erland Sommarskog
from that article:
A special feature added in SQL 2005 is
that you can use EXEC() to run
pass-through queries on a linked
server. This could be another instance
of SQL Server, but it could also be an
Oracle server, an Access database,
Active directory or whatever. The SQL
could be a single query or a sequence
of statements, and could it be
composed dynamically or be entirely
static. The syntax is simple, as seen
by this example:
EXEC('SELECT COUNT(*) FROM ' + #db +
'.dbo.sysobjects') AT SQL2K
SQL2K is here a linked server that has
been defined with sp_addlinkedserver.