I want to PassColumn name as a paremeter in My SP And if my tat column exists in first table (Batch_Master), want to fatch value from that column,
And if that column exists in my second table (GTIN_Master), want to fetch value from tat table column, Each table have columns like.. Batch_Master (Batch_M_id, GTIN(primary key),....etc GTIN_Master (GTIN (foreign key),..etc)
I have Batch_M_id , and column name as a parameter..
Note: column Name having random datatype, some time int or some time datetime etc
I Try followin SP
CREATE PROCEDURE dbo.StoredProcedure2
@columnName varchar(50),
@batchmId int
AS
if exists(select * from sys.columns
where Name = N'columnName' and Object_ID = Object_ID(NBatch_Master'))
begin
select @columnName from Batch_Master
end
else
begin
select @columnName
from GTIN_Master inner join Batch_Master
on GTIN_Master.GTIN = Batch_Master.GTIN
where Batch_M_id =@batchmId
end
RETURN
What you need when you do not know exactly the query structure you are going to execute, then you have to create a dynamic query, which is actually a form of templating queries.
CREATE PROCEDURE dbo.StoredProcedure2
@columnName varchar(50),
@batchmId int
AS
DECLARE @SQL1 AS VARCHAR(MAX)
DECLARE @SQL2 AS VARCHAR(MAX)
SET @SQL1 = 'select ' + @columnName + ' from Batch_Master'
SET @SQL1 = 'select ' + @columnName + '
from GTIN_Master inner join Batch_Master
on GTIN_Master.GTIN = Batch_Master.GTIN
where Batch_M_id =' + CONVERT(VARCHAR,@batchmId)
IF EXISTS(SELECT * FROM sys.columns WHERE Name = @columnName and Object_ID = Object_ID(N'Batch_Master'))
BEGIN
EXEC (@SQL1)
END
ELSE
BEGIN
EXEC (@SQL2)
END
The above will do what you want but is prone to errors. For instance what if the column passed in the parameter does not exist in the tables used in the second query? You should probably need to check for existence in the second case too.
DECLARE @SQL VARCHAR(MAX)
SET @SQL = 'select ' + @columnName + ' from Batch_Master'
EXECUTE(@SQL)
This SQL:
set nocount on
go
create table One (id int, name varchar(25))
go
create table Two (id int, value varchar(25))
go
insert into One values (1, 'Table One')
insert into Two values (1, 'Table Two')
go
create procedure sp_test_colname
@colname sysname
as
declare @sql nvarchar(2048)
if exists (select 1 from sys.columns where name = @colname and object_id = object_id(N'One'))
set @sql = 'select [' + @colname + '] from [One]'
else
if exists (select 1 from sys.columns where name = @colname and object_id = object_id(N'Two'))
set @sql = 'select [' + @colname + '] from [Two]'
if @sql <> ''
exec sp_executesql @sql
go
exec sp_test_colname 'name'
exec sp_test_colname 'value'
exec sp_test_colname 'somethingelse'
go
drop procedure sp_test_colname
drop table One, Two
Returns the following:
name
-------------------------
Table One
value
-------------------------
Table Two
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.