using a variable as xquery sql server 2005 - sql-server

I have a query in sql as following
select PricingXML.query('/Fields/Field1,/Fields/Field2') from T_SMPricingData
PricingXML is a xml column in the table T_SMPricingData.
I need to pass the xquery from a variable to the query like the following
declare #var nvarchar(100)
set #var='/Fields/Field1,/Fields/Field2'
select PricingXML.query('sql:variable("#var")') from T_SMPricingData
How can I use the variable correctly. It is not working in the intended manner
Edit
My output for the original correct query is
<Field1>10</Field1><Field2>11</Field2>
<Field1>20</Field1>
The data in the column looks like
<Fields>
<Field1>20</Field1>
<Field3>22</Field3>
<Field4>23</Field4>
</Fields>
Link for sql fiddle
http://www.sqlfiddle.com/#!3/12d7c/6/0

I achieved the above by
DECLARE #var nvarchar(100)
SET #var='/Fields/Field1,/Fields/Field2'
DECLARE #sqlCommand varchar(1000)
SET #sqlCommand = 'SELECT PricingXml.query('''+#var+''') from MyTable'
EXEC (#sqlCommand)

Related

SQL Select table name

A database had has 4 tables that has the same columns EG. SalesJAn, SalesFeb, SalesMarch, SalesApril.
I want to run a query in SQL server or in report builder Where i can change the table name based on a selection which one of 4 tables will be queried . Eg Filter in report builder
Like this
declare #tablename varchar(50)
set #tablename = 'test'
select * from #tablename
You can create a procedure which will do a select for a given table name. This procedure could look like this:
CREATE PROCEDURE EXECUTE_SELECT
#tbl sysname
AS
BEGIN
SET NOCOUNT ON;
DECLARE #SQL NVARCHAR(MAX);
SET #SQL = N' SELECT * FROM ' + QUOTENAME(#tbl)
EXECUTE sp_executesql #SQL
END
Then you can execute this procedure for every table name you want. Please see here an example according to your description: db<>fiddle
If this doesn't help you, please point out which exactly you still need to know. Thank you.

select with parameter instead of field name

How can I use a parameter instead of a field name like:
Declare #Thecode varchar(10)
Set #Thecode= ‘code’ --'code' is field name.
Select #Thecode from sqltable
Here is the correction in your query:
SET #Thecode= ‘code’ --'code' is field name.
SET #query='Select '+#Thecode+' from sqltable'
EXECUTE(#query)
There are two ways to execute dynamic SQL in SQL Server: use the sp_executesql system stored procedure or the EXECUTE() operator.
And for more details about the dynamic SQL execution Follow the below link:
https://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/
I think you are looking for the Dynamic SQL.
If it is so then please check the following solution:
Note: Tested in SQL Server 2008 R2
Declare #Thecode varchar(10)
DECLARE #query VARCHAR(MAX)
Set #Thecode= 'code' --'code' is field name.
SET #query = 'SELECT '+ #Thecode +' from sqltable';
EXEC(#query)
Always mention the DBMS in the tags while posting the question. And take care of SQL Injection
For more details about Dynamic 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.

How do I use SQL Server table name in select query with a variable?

Is this incorrect, can't we pass the table name to a select query dynamically?
This is giving me a error 'Must declare the table variable #TblName'
DECLARE #TblName VARCHAR(30)
SET #TblName = 'User'
SELECT *
FROM #TblName
You need to create a dynamic SQL query, preferably using the QUOTENAME function. You can avoid any issues from malicious input by using QUOTENAME function.
Here is a sample script that illustrates how to query a table by creating a dynamic SQL query by passing in a table name. You can change the table name by value to the variable #tablename.
Create and insert script for sample:
CREATE TABLE sample
(
id INT NOT NULL
);
INSERT INTO sample (id) VALUES
(1),
(2),
(3),
(4),
(5),
(6);
Dynamic SQL script:
DECLARE #execquery AS NVARCHAR(MAX)
DECLARE #tablename AS NVARCHAR(128)
SET #tablename = 'sample'
SET #execquery = N'SELECT * FROM ' + QUOTENAME(#tablename)
EXECUTE sp_executesql #execquery
Demo:
Click here to view the demo in SQL Fiddle.
Suggested read:
The Curse and Blessings of Dynamic SQL
you have to use dynamic sql execution
wrap your statement in #selectstr
use exec sp_executesql #selectstr
You can do this thing by using dynamic query, Check below
DECLARE #TblName VARCHAR(30)
DECLARE #vQuery NVARCHAR(100)
SET #TblName = 'User'
SET #vQuery = 'SELECT * FROM ' + #TblName
EXECUTE sp_executesql #vQuery

Resources