Tuesday, 29 August 2017

sql server - T-SQL inserting a string into a text datatype column

The answer is to escape your apostrophes and it has to be done if you're going to assign the text to a variable or insert into a database.


Escaping is handled by the query parser not by the processor. When you look at data that was inserted with escaped apostrophes or retrieve it from the database you get it back with single apostrophes. It does not affect your data.


If I understand you properly you're inserting that query into a table so that later you can query that table and execute those queries. Some sore of queued reporting program?


This proof of concept query proves this is possible WITH ESCAPED APOSTROPHES. I didn't have your schema so I had to change the names of the tables.
It creates the necessary tables and a mock bookstore procedure that inserts the ESCAPED input_query into a table. This is repeated 3 times with 3 different queries. Then I use a temporary table and while loop to pull those queries and execute them.


drop table t1;
create table t1 (
name varchar( 20 ),
id varchar( 20 ),
date varchar( 20 ),
rid int identity primary key
)
drop table t2;
create table t2 (
name varchar( 20 ),
id varchar( 20 ),
[period] datetime,
def varchar( 10 ),
rid int identity primary key
)
drop table QueryData;
create table QueryData (
query nvarchar(max),
rid int identity primary key
)
go
---- drop procedure dbo.booksellers;
go
create procedure dbo.booksellers (
@input_query nvarchar(max)
)
as
begin
declare @return int
insert into QueryData ( query ) values ( @input_query )
select @return = SCOPE_IDENTITY()
return @return
end
go
insert into t1 ( name, id, date ) values
( 'row1', 'some', 'period' ),
( 'row2', 'more', 'period' ),
( 'row3', 'even', 'not period' )
insert into t2 ( name, id, [period], def ) values
( 'period1', 'some', '2016-09-15 06:00', 'u' ),
( 'period2', 'more', '2016-09-15 07:00', 'u' ),
( 'period3', 'less', '2017-09-15 06:00', 'u' ),
( 'period1', 'some', '2017-09-15 06:00', 'u' ),
( 'period2', 'more', '2017-09-15 07:00', 'u' ),
( 'period3', 'less', '2017-09-15 06:00', 'u' ),
( 'period1', 'some', '2017-09-15 06:00', 'x' ),
( 'period2', 'more', '2017-09-15 07:00', 'x' ),
( 'period3', 'less', '2017-09-15 06:00', 'x' )
DECLARE @return_value int
declare @input_query nvarchar(max)
set @input_query = N'Select t1.*, t2.name from t1 t1 right join t2 t2 on
t1.id = t2.id where t1.date = ''period'' and t2.period between ''2016-01-01'' and ''2016-12-31'' and t2.def = ''u'''
EXEC @return_value = [dbo].[booksellers] @input_query
print 'query'
exec sp_executesql @input_query
SELECT 'Return Value' = @return_value
set @input_query = N'Select t1.*, t2.name from t1 t1 right join t2 t2 on
t1.id = t2.id where t1.date = ''period'' and t2.period between ''2017-01-01'' and ''2017-12-31'' and t2.def = ''u'''
EXEC @return_value = [dbo].[booksellers] @input_query
exec sp_executesql @input_query
SELECT 'Return Value' = @return_value
set @input_query = N'Select t1.*, t2.name from t1 t1 right join t2 t2 on
t1.id = t2.id where t1.date = ''period'' and t2.period between ''2017-01-01'' and ''2017-12-31'' and t2.def = ''x'''
EXEC @return_value = [dbo].[booksellers] @input_query
exec sp_executesql @input_query
SELECT 'Return Value' = @return_value
select 'Queued Queries', * from QueryData
declare @queries table (
queryId int,
rid int identity
)
insert into @queries ( queryId )
select rid from QueryData
declare @row int
declare @queryId int
declare @queryText nvarchar(max)
select @row = max(rid) from @queries
while @row > 0
begin
select @queryId = queryId from @queries where rid = @row
select @queryText = query from QueryData where rid = @queryId
select @queryText
exec sp_executesql @input_query
delete @queries where rid = @row
set @row = @row -1
end

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...