0

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?

15
  • 2
    learn.microsoft.com/en-us/sql/relational-databases/… or mssqltips.com/sqlservertip/1160/… Commented Jul 28, 2022 at 8:53
  • Please move your suggestion as answer. I will mark it as answer. From your source I understand that I need to call it as like exec(@SQLQueryString). Commented Jul 28, 2022 at 8:58
  • What value is the stored procedure providing here? Commented Jul 28, 2022 at 9:02
  • 2
    This smells like an XY Problem. Passing a string to just execute is a HUGE security vulnerability. What controls do you have to stop a user running DROP TABLE dbo.MyTable or CREATE LOGIN a WITH PASSWORD = '1', CHECK_POLICY = OFF; ALTER SERVER ROLE sysadmin ADD MEMBER a;? Commented Jul 28, 2022 at 9:04
  • 1
    Also there is no "Microsoft SQL Server 2015" Commented Jul 28, 2022 at 9:07

1 Answer 1

0

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/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.