I try to bulk insert different files.
When i code it like:
SET #xml=( Select * From OPENROWSET(
BULK 'C:\Data\csvToXml.xml',SINGLE_BLOB)x)
it is working.
If i take path as parameter like:
SET #Path= 'C:\Data\csvToXml.xml'
SET #SqlStmt= N' Select #xmlDoc=( Select * From OPENROWSET(
BULK '''+#Path+''' ,SINGLE_BLOB)x)'
exec sp_executesql #SqlStmt, N'#xmlDoc XML',#xmlDoc
#xmlDoc seems empty. I cannot find where I'm wrong.
Thanks for help.
Came here looking for a solution to my own problem but ended up solving this one!
DECLARE #Output int = 0;
DECLARE #params nvarchar(max) = N'#DesiredValue int OUTPUT';
DECLARE #sql_string nvarchar(max) = N'SELECT #DesiredValue = 13';
EXECUTE sp_executesql #sql_string, #params, #DesiredValue = #Output OUTPUT
SELECT #Output -- yields 13
It turns out you need to pass the keyword OUTPUT not only in the #params argument but also on the variables you wish to retrieve. The "#DesiredValue = #Output OUTPUT" looks a little odd because #Output is taking the value of #DesiredValue rather than the other way around.
Documentation.
Related
EDIT: SQL Server Version
I'm trying to pass this variable into my open query using this guide from Microsoft: Link
I'm running into this error message "Statement(s) could not be prepared." Which I believe means something is wrong with the OpenQuery. I'm just not sure what is wrong.
Here's the code:
DECLARE #ticketid INT, #QLFD VARCHAR(8000)
SELECT #ticketid = '296272348'
SELECT #QLFD = 'SELECT
*
FROM
OPENQUERY(
[Server_name],''
SELECT
ticket_id
, QLFD_SPD_AMT
FROM [database].[dbo].[table]
WHERE ticket_id = #ticketid
'')'
EXEC (#QLFD)
Could you help me identify the error? I prefer to do it passing the whole query as one.
Thanks!
Edit:
After looking at suggestions made by #Larnu. I have adjusted my code to:
DECLARE #ticketid INT--, #QLFD NVARCHAR(Max)
SELECT #ticketid = '296272348'
DECLARE #QLFD NVARCHAR(Max) = 'SELECT
*
FROM
OPENQUERY(
[Server_name],''
SELECT
ticket_id
, QLFD_SPD_AMT
FROM [database].[dbo].[table]
WHERE ticket_id = QUOTENAME(#ticketid, '''''''')
'')';
EXEC (#QLFD);
As I mentioned, you can't parametrise a query with OPENQUERY you have safely inject the values.
Normally that would be with QUOTENAME or REPLACE, but you don't actually need to do that here, due to the value being a numerical data type, so you can just concatenate it in:
DECLARE #ticketid int = 296272348; --Don't wrap numerical datatypes with quotes.
DECLARE #SQL nvarchar(MAX),
#OpenQuery nvarchar(4000);
SET #OpenQuery = CONCAT(N'SELECT QLFD_SPD_AMT
FROM [database].[dbo].[table]
WHERE ticket_id = ',#ticketid,N';'); --As it's an int we dont need to quote
SET #SQL = CONCAT(N'SELECT #ticketid AS ticket_id, QLFD_SPD_AMT
FROM OPENQUERY([servername],N''',REPLACE(#OpenQuery,'''',''''''),N''');';
EXEC sys.sp_executesql #SQL, N'#ticketid int', #ticketid;
I am doing a procedure that accepts an input parameter of a filepath, reads the json data from it and inserts the data into a table. However, when I try to execute the procedure, I get this error:
Msg 201, Level 16, State 4, Procedure main.loadData, Line 0 [Batch Start Line 244]
Procedure or function 'loadData' expects parameter '#filePath', which was not supplied.
The surprising thing is that I am adding a parameter..
The code for the procedure is this:
ALTER PROCEDURE main.loadData
(#filePath VARCHAR(200))
AS
BEGIN
DECLARE #pathScript AS VARCHAR(MAX)
SET #pathScript='DECLARE #jsonVariable NVARCHAR(max);
SELECT #jsonVariable = BulkColumn
FROM OPENROWSET (BULK ''' + #filepath
+ ''', SINGLE_CLOB) as j;
INSERT INTO main.jsonData(restaurant, priceRange, country, score, reviewDate)
SELECT *
FROM OPENJSON(#jsonVariable, ''$.reviews.row'')
WITH
(restaurant VARCHAR(100) ''$.restaurant'',
priceRange VARCHAR(50) ''$.priceRange'',
country VARCHAR(50) ''$.country'',
score INTEGER ''$.score'',
reviewDate DATETIME ''$.reviewDate''
);';
EXEC(#pathScript);
END;
GO
And the execution code is this:
EXEC main.loadData 'C:\data.json';
Try setting a parameter in the declaration.
ALTER PROCEDURE main.loadData
#filePath VARCHAR(200) NOT NULL
AS
BEGIN
DECLARE #pathScript AS VARCHAR(MAX), #params VARCHAR(200);
SET #params = #filepath VARCHAR(200);
SET #pathScript='DECLARE #jsonVariable NVARCHAR(max);
SELECT #jsonVariable = BulkColumn
FROM OPENROWSET (BULK ''' + #params
+ ''', SINGLE_CLOB) as j;
INSERT INTO main.jsonData(restaurant, priceRange, country, score, reviewDate)
SELECT *
FROM OPENJSON(#jsonVariable, ''$.reviews.row'')
WITH
(restaurant VARCHAR(100) ''$.restaurant'',
priceRange VARCHAR(50) ''$.priceRange'',
country VARCHAR(50) ''$.country'',
score INTEGER ''$.score'',
reviewDate DATETIME ''$.reviewDate''
);';
EXEC(#pathScript);
END;
GO
And try executing like this
EXEC main.loadData #filepath = 'C:\data.json'
I found the problem. And it's the silliest problem.. Part of my program has a trigger which fires after this procedure inserts the data into the table. Now, to avoid inserting multiple times on the same table (meaning if u execute 2 times, you get double the data), i added a DELETE FROM main.jsonData, and then EXEC main.loadData (which at time didn't need any parameters). Due to this execution line, which didnt give the parameter, it gave me the error. Thanks to everyone that helped!
I am sure this is easy but I have a select statement stored as a variable e.g. #statement contains a statement "select count(*) from table1" I need to execute this variable e.g. sp_executesql #statement but put the results in to a temp table, is this possible easily?
Thanks
You can create your temp table first, then use insert into ... exec...:
declare #statement nvarchar(max);
set #statement = 'select 1';
create table #temp (rc int);
insert into #temp (rc)
exec sp_executesql #statement;
select * from #temp;
rextester demo: http://rextester.com/WPDAZ22362
returns: 1
One way:
declare #statement nvarchar(max) = 'select count(*) from table1'
declare #sql nvarchar(max) = N'set #result = (' + #statement + ')'
declare #result int
exec sp_executesql #sql, N'#result int OUTPUT', #result=#result OUTPUT;
select #result;
select #result as count into #temp
Sorry to say I wasn't given all of the question, where in the select statement I posted it only returned 1 field but I have now been told that the count i.e. 1 field was just an example and I might need to return multiple fields so SqlZim response would not of worked I don't think as I wouldn't always know the number/name of columns. What I chose to do was the following :-
Declare #TempSQLStatement nvarchar(max)
SET #TempSQLStatement=REPLACE(#statement,' FROM ',' INTO ##temp FROM ');
EXEC sp_executesql #TempSQLStatement;
SELECT * from ##temp
Not sure how to award the answer as Alex's solution would work fine and I believe SqlZim would of posted something similar if I had posted the complete info in the first place, can admin assist?
I need your help please,
I read the forum but I'm still having a persistant problem without solution. I want to pass a variable into parameters of an SP_EXECUTESQL, but the result value of the passed variable is always ZERO in my SQL statement.
Here is my code :
declare #counter int
declare #sql nvarchar(max)
// ------ My #COUNTER value I want to get, it's ok
SET #counter = (
select COUNT(CREATED_DT)
from USERS
where CAST(CREATED_DT as date) = CAST(#DATE_TIME as date)
)
// ------ I try to pass my #COUNTER value for my COUNTER_COLUMN, and it's not ok anymore...
SET #sql = N'update DATA_REPORT set COUNTER_COLUMN = #counterB';
EXEC sp_executesql #sql, N'#counterB int', #counterB = #counter;
I do not have errors on execution but in my SQL there is always :
COUNTER_COLUMN = 0 !!! I checked before that my #counter was not returning 0, and it returns perfectly miscenaneous good values.
Any clues about this issue ?
Thank you very much :)
Robin
Have you tried this:
declare #sql nvarchar(max)
SET #sql = N'update myTable set avg = (
select COUNT(CREATED_DT)
from USERS
where CAST(CREATED_DT as date) = CAST(#DATE_TIME as date)
)';
EXEC sp_executesql #sql
Greatly simplifying this to remove the unnecessary dynamic sql it would like something like this.
update DATA_REPORT
set COUNTER_COLUMN =
(
select COUNT(CREATED_DT)
from USERS
where CAST(CREATED_DT as date) = CAST(#DATE_TIME as date)
)
From your query, i try to use 'concat' the parameter instead of using #counterB and it work just fine.
declare #sql nvarchar(max)
declare #counter int = 1
set #sql = concat(N'update DATA_REPORT set COUNTER_COLUMN = ', #counter);
exec sp_executesql #sql
#sql look like this
update DATA_REPORT set COUNTER_COLUMN = 1
Thanks to all of you,
Your answers were very interesting and helped me to think differently and to solve my issue. I choosed to execute my statement the more simple as possible without #parameters with sp_executesql. It made problems with the TYPE of my variables.
PS. #Rawitas Krungkaew > I didn't try CONCAT but keep it in mind, thk you.
declare #dyn_col_account varchar(40);
declare #DATE_TIME nvarchar(50)
SET #counter = (
select COUNT(CREATED_DT)
from USERS
where CAST(CREATED_DT as date) = CAST(#DATE_TIME as date)
)
DECLARE #query1 nvarchar(max) = '
UPDATE DATA_REPORT
SET ['+#dyn_col_account+'] = '+ #counter +'
WHERE TIME_DAY=cast('''+#DATE_TIME+''' as date)'
EXECUTE sp_executesql #query1
Maybe the solution can be so easy but I can't find it so I write here for some help.
We have this sql function:
CREATE FUNCTION [dbo].[updateExcel]
(
-- Add the parameters for the function here
#cell VARCHAR(4),
#description VARCHAR(200)
)
RETURNS BIT
AS
BEGIN
DECLARE #sql NVARCHAR(1000)
SET #sql = 'UPDATE openquery(LinkedServer2ExcelFile, ''SELECT * FROM [Sheet1$'+#cell+':'+#cell+']'') set F1 = '''+#description+''''
--PRINT #sql
EXEC sp_executesql #sql
RETURN 0
END
that we use to update some excel file
EXEC #Result = updateExcel 'somecell', 'somevalue'
The problem is that after this update the excel has a bigger size. But when we open it and save it again, the file's size get normal again
I hope to find here some answers ...
Thanx !!!