Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 15264

How to pass multiple table in dynamic sql in sql server 2088 r2?

$
0
0

Hi ,

i got a requirement like the below

i can able to pass one table name as dynamically in dynamic sql. but i need to pass multiple tables in dyanamic sql and i need to include the table names what ever i am passsing same thing needs to be include as column name
Ex:
empno empname deptname tablename
101    krishna Research emp,dept

or
101    krishna research  emp-dept
to pass one table i am using the below code

ALTER PROCEDURE Dynamic_SP
      @Table_Name sysname
AS
BEGIN
      SET NOCOUNT ON;
      DECLARE @DynamicSQL nvarchar(4000)
      SET @DynamicSQL = N'SELECT * FROM  ' + @Table_Name
      EXECUTE sp_executesql @DynamicSQL
END
GO

EXEC Dynamic_SP 'EMP'

now i need to convet the below query into dynamic sql i.e

select empno,empname,deptname,emp-dept as tablename from emp inner join dept on emp.deptno=dept.deptno

Thanks in advance


franklinsentil


Viewing all articles
Browse latest Browse all 15264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>