dynamic sql statement insertion issue - sql-server

I have created a Temp table(#TempTable). I am trying to insert the date to get but I am getting an error. It's a dynamic query.
I am trying to get the date from another table and inserting the date to temp table
Just to make sure you understand the problem I have given an example
DECLARE #OfferEndDateTime datetime
SELECT #OfferEndDateTime = getdate()-1
print #VOfferEndDateTime
DECLARE #SQL VarChar(1000)
SELECT #SQL ='INSERT INTO #TempTable '+
'SELECT D,Points,#OfferEndDateTime '
exec(#sql)
Please Let me know where I am going wrong

You need to use sp_executesql when passing a parameter to dynamic sql
exec sp_executesql #sql, N'#OfferEndDateTime datetime', #OfferEndDateTime=#OfferEndDateTime

You have, at least, three problems:
The variable needs to be outside:
SELECT #SQL ='INSERT INTO #TempTable '+
'SELECT D,Points,' + #OfferEndDateTime
The variable needs to be varchar type or similar
What is D,Points? They are not defined anywhere. If they are varchar values you nead to quote them (use " or '') for that purpose.
If you need to use the parameter like datetime you should use sp_executesql instead. Check HERE for some info on it!

Related

T-SQL Error Message 207 When passing values Dynamically

I am attempting to create a stored procedure that allows values to be passed to it, however, when I try to use Dynamic T-SQL I raise an error MSG 207
I know that the error message is supposed to indicate that the column name was misspelled in my statement thanks to the following site:
https://www.tsql.info/error/msg-207-level-16-invalid-column-name.php
This, however, does not make sense as the following hard coded statement works just fine.
INSERT INTO [dbo].[tDriversLicense] (DriversLicensePrefix, DriversLicenseSuffix, DateAdded, DriversLicenseNumber)
VALUES ('shockc', '11653798', GETDATE(), 'GAD4859');
ALTER PROCEDURE [dbo].[spAddDriversLicense]
-- Add the parameters for the stored procedure here
#DriversLicensePrefix NCHAR(8),
#DriversLicenseSuffix NCHAR(10)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE #sqlInsertDriversLicense NVARCHAR(MAX)
SET #sqlInsertDriversLicense = 'INSERT INTO [dbo].[tDriversLicense] (DriversLicensePrefix, DriversLicenseSuffix, DateAdded, DriversLicenseNumber)
VALUES (' + #DriversLicensePrefix + ', ' + #DriversLicenseSuffix + ', GETDATE(), GPC4859);'
EXECUTE sp_executesql #sqlInsertDriversLicense
END
I've been spilling over this code for hours and I'm thoroughly confused as to why there is any difference between the two INSERT statements. The values being passed are the same but an error is raised when I attempt to pass them dynamically. How is this raising an error??
I've used some code from this video to attempt to learn how pass Scalar Values dynamically and have had luck in other stored procedures.
https://www.youtube.com/watch?v=RHHKG65WEoU
When working with dynamic strings, select them first, so you can check that the resulting syntax is workable.
You need single quotes in the result, so you need more than single, single quotes, in the code:
declare #DriversLicensePrefix nchar(8) = 'A2345678'
declare #DriversLicenseSuffix nchar(10) = 'abcdefghij'
DECLARE #sqlInsertDriversLicense nvarchar(max)
SET #sqlInsertDriversLicense = 'INSERT INTO [dbo].[tDriversLicense] (DriversLicensePrefix, DriversLicenseSuffix, DateAdded, DriversLicenseNumber)
VALUES (N''' + #DriversLicensePrefix + ''', N''' + #DriversLicenseSuffix + ''', ''' + convert(char(8),GETDATE(),112) + ''', ''GPC4859'');'
select #sqlInsertDriversLicense
+-------------------------------------------------------------------------------------------------------------------+
| result: |
+-------------------------------------------------------------------------------------------------------------------+
| INSERT INTO [dbo].[tDriversLicense] (DriversLicensePrefix, DriversLicenseSuffix, DateAdded, DriversLicenseNumber) |
| VALUES (N'A2345678', N'abcdefghij', '20181109', 'GPC4859'); |
+-------------------------------------------------------------------------------------------------------------------+
NB You should use convert(char(8),getdate(),112) SQL Server will recognize the YYYYMMDD format as a date value regardless of server default settings.
The result see above demonstrates what the insert statement MUST be, note that it it contains several single quotes.
When you are concatenating the SQL statement, you are also dealing with strings, and every part of that has to be contained within single quotes.
So there are multiple needs for single quotes.
And; So you need multiple single quotes throughout out the concatenation, some to help form the SQL statement, and others to be INSIDE that statement.
/* get this into #sql */
select 'Y' as col1;
declare #SQL as nvarchar(max)
set #SQL = N'select ''Y'' as col1;'
select #SQL;
+---------------------+
| #SQL |
+---------------------+
| select 'Y' as col1; |
+---------------------+
In the larger query 2 variables are defined a NCHAR(8) or (10) as you have defined them a Nchar then when inserting data into those you should prefix that input by N
As posted in a comment already there is no need for this dynamic approach at all.
The following code will show a straight and a dynamic approach. Try it out:
USE master;
GO
CREATE DATABASE testDB;
GO
USE testDB;
GO
CREATE TABLE TestTable(SomeContent VARCHAR(100),SomeDate DATETIME,SomeFixString VARCHAR(100));
GO
--This procedure will use the parameters directly. No need for any dynamic SQL:
CREATE PROCEDURE TestStraight(#content VARCHAR(100))
AS
BEGIN
INSERT INTO TestTable(SomeContent,SomeDate,SomeFixString)
VALUES(#content,GETDATE(),'Proc Straight');
END
GO
--this procedure will use real parameters. You should never use parameters in string concatenation. One day you might meet bobby tables...
CREATE PROCEDURE TestDynamic(#content VARCHAR(100))
AS
BEGIN
DECLARE #cmd NVARCHAR(MAX)=
N'INSERT INTO TestTable(SomeContent,SomeDate,SomeFixString)
VALUES(#DynamicContent,GETDATE(),''Proc Dynamic'');'
EXEC sp_executesql #cmd,N'#DynamicContent VARCHAR(100)',#DynamicContent=#content;
END
GO
--Test it
EXEC TestStraight 'yeah!';
EXEC TestDynamic 'oh yeah!';
SELECT * FROM TestTable;
GO
--Clean up
USE master;
GO
--careful with real data!
--DROP DATABASE testDB;

How to use the value of a SELECT as a column name in a subquery

I ask for your help because I can not determine a solution to this problem. I am using SQL Server 2014.
I have a query like this:
SELECT
event_id,
CONCAT ('week', CHARINDEX('Y', weeks), '_date') AS num
FROM
CT_EVENT
which returns a week number of the form: week1_date
This value is the name of a column in another table. What I would like to do is a subrequest to get the value that is in it.
SELECT week1_date
FROM CT_CONFIG
I looked for a way to 'caster' a string in column name without actually finding.
Thank you in advance, and remain available for any supplement.
Jérémy
You cannot use Sub-query here, you will need to use Dynamic sql, something like this...
Declare #ColumnName SYSNAME
, #Sql NVARCHAR(MAX);
SELECT #ColumnName = CONCAT ('week', CHARINDEX ('Y', weeks), '_date')
FROM CT_EVENT
SET #Sql = N' SELECT ' + QUOTENAME(#ColumnName) + N' FROM CT_CONFIG;'
Exec sp_executesql #Sql;

How to pass a variable value in a FROM clause in Microsoft TSQL

sorry I am uploading a pic of my query as I dont know to format my text...as a newbie its confusing.
So you want to execute:
select * from File_20170703 -- where your table name is a variable.
It is not possible to use variables for table or column names, what you need to do is to build a dynamic sql and execute it using sp_executesql.
here is an example:
DECLARE #sql nvarchar(4000)
SELECT #sql = 'SELECT * FROM File_20170703'
EXEC sp_executesql #sql
More info about dynamic sql
A simple TSQL "dynamic sql" looks like this:
DECLARE #file_name AS VARCHAR(100)
DECLARE #query AS VARCHAR(MAX)
SET #file_name = 'file_20170101'
SET #query = 'SELECT * FROM ' + #file_name
execute(#query)
Basically you need to create a valid sql query by concatenating various parts of the query together, then you can execute that whole big string as your query.
You can use SQL Cursor along with while loop. Examples are given here:
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/while-transact-sql

SQL - Can I pass a variable to be used in the FROM statement?

Can I pass a variable to a SELECT statement?
I keep getting an error message saying I need to declare it.
However, it is declared.
SELECT (list of columns)
FROM #database_table
You are looking to use Dynamic SQL to perform this type of query.
The Curse and Blessings of Dynamic SQL
Here is a quick sample
declare #sqlstatement nvarchar(4000)
declare #table sysname
set #table = 'yourTableName'
set #sqlstatement = 'SELECT * FROM ' + QUOTENAME(#table)
exec(#sqlstatement)
Yes, use dynamic sql statements to build your select statement.
-- Procedure input parameters
#TableName varchar(50)
-- Query guts
Declare #sql varchar(2000)
Set #sql = 'Select columnname from ' + #TableName
exec (#sql)
The one time you can do what you want is when you use table variables. You have to define the variables as:
declare #name table (<column list>)
This is alternative method of declaring a temporary table.
Other than this, I fully agree with bluefeet. You should read the link he posted.

Use Parameter In OPENROWSET Sql Server

I am creating an stored procedure in which I am calling an another
stored procedure(This procedure is returned lot of columns and I want
only one column value So I can't create temp table to store values)
using OPENROWSET.
When I am use following then it's alright
declare #AgencyID int=15,#PatientID int=3701
SELECT a.PrimaryInsuredName
FROM OPENROWSET('SQLNCLI',
'Server=ServerName;Database=DbName;Trusted_Connection=yes',
'exec USP_Billing_GetPatientWithInsurence 3701,15') AS a;
It's working fine. But I want to pass parameters for calling
USP_Billing_GetPatientWithInsurence because values will be dynamic.
So I use following code
declare #AgencyID int=15,#PatientID int=3701
SELECT a.PrimaryInsuredName
FROM OPENROWSET('SQLNCLI',
'Server=ServerName;Database=DbName;Trusted_Connection=yes',
'exec USP_Billing_GetPatientWithInsurence '+ #PatientID +','+ #AgencyID+'') AS a;
But it's not working When I run this query then an error occurred
Incorrect syntax near '+'. I don't know why this is coming. Please
provide a solution to this. I googled also for this but can't found a
proper solution.
Thanks
You have to make your entire SELECT string dynamic:
declare #AgencyID int=15,#PatientID int=3701
DECLARE #SQLStr varchar(max)='
SELECT a.PrimaryInsuredName
FROM OPENROWSET(''SQLNCLI'',
''Server=ServerName;Database=DbName;Trusted_Connection=yes'',
''exec USP_Billing_GetPatientWithInsurence '+ CAST(#PatientID AS varchar(15)) +','+ CAST(#AgencyID AS varchar(15)) +''') AS a';
EXECUTE(#SQLStr);

Resources