Translate

Wednesday, 13 May 2015

sp_executesql having multiple parameters


create table   Students
 (
 StudentId int primary key not null
 ,StudentName varchar(200) not null  
  )
insert into Students(StudentId,StudentName)values(1,'Pabitra Behera')
insert into Students(StudentId,StudentName)values(2,'Pabitra')
insert into Students(StudentId,StudentName)values(3,'d')
insert into Students(StudentId,StudentName)values(1,'Pabitra')

DECLARE @ExecStr NVARCHAR(4000),@studentId int,@Name varchar(50) ;
set @studentId=1
set @Name='Pabitra Behera'
SELECT @ExecStr = 'SELECT * FROM dbo.Students where StudentId=1 and StudentId=@studentId and StudentName=@Name';
EXEC sp_executesql @ExecStr,N'@studentId int,@Name varchar(50)',@studentId,@Name

--------------------------------------------------------------------------------------

DECLARE @ExecStr NVARCHAR(4000),@studentId int,@Name varchar(50) ;
set @studentId=1
set @Name='Pabitra Behera'
SELECT @ExecStr = 'SELECT * FROM dbo.Students where StudentId=@studentId and StudentName=@Name';
EXEC sp_executesql @ExecStr,N'@studentId int,@Name varchar(50)',@studentId,@Name
--------------------------------------------------------------------------------------

No comments:

Post a Comment