Using openrowset in SQL Server 2014 - sql

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?

Related

Linked servers, results query xp_cmdshell to table migration SQL Server 2005 to SQL Server 2019

I'm migrating procedures from Microsoft SQL Server 2005 to Microsoft SQL Server 2019 and I got stuck while trying insert query result xp_cmdshell in linked servers to table
I'm out of ideas
Old solution in Microsoft SQL Server 2005:
INSERT INTO LOGTABLE (ShopNo,Line) SELECT 1, OUTPUT FROM openquery ([IP_LINKED_SERV],'set fmtonly off; exec master..xp_cmdshell ''type d:\log\file.log'' ')
Microsoft SQL Server 2019 gives me error:
Msg 11519, Level 16, State 1, Procedure
sp_describe_first_result_set, Line 1 [Batch Start Line 0] The metadata could not be determined because statement 'exec master..xp_cmdshell 'type d:\log\file.log'' invokes an extended stored procedure.`
I found a way how to do xp_cmdshell in SQL Server 2019 at linked servers
EXEC ('set fmtonly off;exec master..xp_cmdshell ''type d:\log\file.log'' ') AT [IP_LINKED_SERV]
However I can't insert this results in table
INSERT INTO LOGTABLE (ShopNo,Line) SELECT '998', OUTPUT FROM EXEC ('set fmtonly off;exec master..xp_cmdshell ''type d:\log\file.log'' ') AT [IP_LINKED_SERV]
Incorrect syntax near the keyword 'EXEC'.
part of the procedure in sql2005:
DECLARE TableCursor CURSOR FOR
SELECT IP, SqlUser, SqlPass, Object FROM ..ObjectInfo
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO #Ip, #SqlUser, #SqlPass, #Object
WHILE ##FETCH_STATUS = 0
BEGIN
PRINT #Object
SELECT #PARAMS = ' #tmp_object varchar(5) OUTPUT'
set #SQL = 'INSERT INTO LOGTABLE (Object,Line) SELECT #tmp_object, output FROM openquery (['+#Ip+'],''set fmtonly off;
exec master..xp_cmdshell ''''type d:\log\file.log''''
'')'
BEGIN TRY
EXECUTE sp_executesql #SQL,#PARAMS, #tmp_object = #Object OUTPUT
END TRY
BEGIN CATCH
INSERT INTO LOGTABLE (Object, Line) VALUES(#Object, '-error')
END CATCH```

How to get output of dynamic SQL exported in excel using MS SQL Server Management Studio 2012?

I have a query that gives all the details about each table in the schema. But as it is dynamic SQL I am not able to export it's result from SQL Server management Studio 2012. The SQL is:
Declare #sql Nvarchar(MAX)
Set #sql = N''
Select #sql = #sql + N'
EXEC sp_help ''['+TABLE_SCHEMA+N'].['+TABLE_NAME+N']'';'
From INFORMATION_SCHEMA.TABLES
Print N'Executing:
'+#sql+'
'
EXEC(#sql)
When I try export feature from SQL server Management Studio 2012 I get error as:
Is there a way I can get output of this query exported in a xls sheet. I can change the query as well as far as it gives details (sp_help tablename) of all the tables present in the schema.

Dynamic SQL query string truncated to 256 characters

I am trying to run dynamic SQL in SQL Server 2016, like this:
declare #SQL varchar(MAX);
set #SQL='SELECT top 1 * INTO Table 1 FROM
OPENROWSET(''Microsoft.ACE.OLEDB.12.0'', etc... (string aprox. 450 char)
EXECUTE sp_executesql #SQL;
For some reason, the variable #SQL is truncated to 256 characters. I already followed instructions in the article below and modify some SQL Server query settings but no result. See:
https://www.mssqltips.com/sqlservertip/2795/prevent-truncation-of-dynamically-generated-results-in-sql-server-management-studio/
Can you please advise, not sure what I am missing, it didn't happen in SQL Server 2008 that I used before. is there any additional setting in SQL Server 2016?
The problem is the sp_executesql uses nvarchar, so you should declare #sql as such, and also SET #sql = N'....:
DECLARE #SQL nvarchar(MAX);
SET #SQL = N'SELECT top 1 * INTO Table 1 FROM
OPENROWSET(''Microsoft.ACE.OLEDB.12.0'', etc... '
EXECUTE sp_executesql #SQL;

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

Execute sybase stored procedure as linked server procedure sql server 2008

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)