Passing query string through variable value fails [duplicate] - sql

This question already has answers here:
Use dynamic table name in sql server query
(2 answers)
Closed 7 months ago.
Using Microsoft SQL Server 2015
This works
create procedure Checking1(#SQLQueryString nvarchar(max))
as
begin
select * from MyTable
end
This is not working
create procedure Checking1(#SQLQueryString nvarchar(max))
as
begin
#SQLQueryString
end;
What am I doing wrong?

You need to tell SQL Server to execute your dynamic-SQL
https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-executesql-transact-sql?view=sql-server-ver16
or
https://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/

Related

Run SQL Dynamic using column value [duplicate]

This question already has answers here:
T-SQL: How to use parameters in dynamic SQL?
(4 answers)
sql stored procedure argument as parameter for dynamic query
(2 answers)
SQL Server 2017 - How to pass a parameter in a SELECT inside a dynamic SQL
(1 answer)
Closed last year.
I am using dynamic SQL.
I inserted a select statement into one row from #example, I want to call that select statement and run it with dynamic SQL, my select stored has a variable call #NumOrder
create table #example(id int, description varchar(1000))
insert into #example(id, description)
values(1, 'select case when name=''Carlos'' then ''Pass'' else ''FAIL'' from server.dbo.person where number = #NumOrder')
declare #NumOrder int, #SQL nvarchar(max)
set #NumOrder=2621
set #SQL='description'
--here is where I want to call my query and is failing
--once I filled #NumOrder I want to display the results
exec(#SQL)
I want to run this dynamic SQL using a row stored into the temp table, I know that I can put the complete select statement into the #SQL and works but I want to know if there is a way to call the select statement in a row stored from a table. Is there a way to do this?

SQL Server Stored Procedure Return value Assign to a Variable [duplicate]

This question already has answers here:
How to assign an exec result to a sql variable?
(7 answers)
Closed 7 years ago.
I am trying to execute a SP inside another SP. Example below.
declare #deal_1 as int
declare #deal_2 as int
set #deal_1 = (EXEC [my second SP] #para1 = 'xxxxx' ) --this returns single value
set #deal_2 = (some other sub query)
select #deal_1, #deal_2
My question is above should return simple two column results but I can't get this to work. Unable to save due to errors or syntax issues.
Error I am getting is "Incorrect syntax near the keyword 'EXEC'."
Your select statement is unable to bind the columns deal_1 and deal_2 and there are no tables to bind to (i.e. no FROM clause in your select). Perhaps you meant to select the values of the parameters (note the # symbols)?
select #deal_1 as deal_1, #deal_2 as deal_2

Creating a SQL Use statement for cross server query [duplicate]

This question already has answers here:
Set database name dynamically in SQL Server stored procedure?
(3 answers)
Closed 9 years ago.
How do you create a cross server query use statement to run a select. This is what I have but I cant seem to get anything than 'does not exist'
Thanks!
DECLARE #ServerVar AS NVARCHAR (MAX)
SET #ServerVar = 'servera/server1';
DECLARE #DBVar AS NVARCHAR (MAX)
SET #DBVar = 'db';
Declare #reportVar nvarchar(max);
Set #reportVar = 'USE ' + quotename(#ServerVar) + '.' + quotename(#DBVar);
EXEC (#reportVar);
SELECT * FROM myTable;
You need to link the remote server to your local one. After that you can query remote server from your local one even without using Use command
This is a good time to use a Linked Server and possibly even synonyms and dynamic sql.
See similar post: Set database name dynamically in SQL Server stored procedure?

Debugging stored procedure in sql server [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Debugging Stored Procedure in SQL Server 2008
I wanted to debug the stored procedure in sql server. I tried with break points, but that didn't work for me. I searched in stack over flow even but din't get proper answer. My procedure is as follows,
USE [Practice]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[selectsample]
#input varchar(100),
#output int output
as
begin
select #output=id
from people where name=#input order by id desc
end
What is the best way to debug this stored procedure?
Try adding top 1 to your query. You have one output patameter but your query can return multiple values (what if there are more people that meet name=#input criteria)

Passing Multiple Parameters to SQL Server [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parameterizing an SQL IN clause?
Comma-separated value insertion In SQL Server 2005
I'm trying to search in my database using where in clause, but my string is in follow format:
'233052,57516351,254689'
I need to do an consult in my database using the following query:
SELECT * FROM myTable WHERE field IN (#list_string)
How I do to make this action?
Use Table-valued parameters, introduced in SQL Server 2008.
These let you pass in a table structure that you can use to query on.
For other options, I suggest reading Arrays and Lists in SQL Server, by Erland Sommarskog.
I've been using Itai Goldstein's Split function for years for this very situation. You could then do the following:
SELECT *
FROM [myTable]
WHERE [field] IN (
SELECT [Data]
FROM [dbo].[Split] (#list_string, ',')
);
Try EXEC of sql statement concatenation:
declare #sql varchar(200)
set #sql='SELECT * FROM myTable WHERE field IN ('+#list_string+')'
exec(#sql)