Scenario: Some times we want to make TOP Count as Dynamically then passing parameter to TOP command will not work. We have to add parameter with parenthesis[ ]
IF OBJECT_ID('[dbo].[sample]','U') IS NOT NULL
DROP TABLE dbo.sample
CREATE TABLE dbo.sample
(ID int,
name varchar(10));
INSERT INTO dbo.sample
SELECT 1,'a'
UNION
SELECT 2,'b'
UNION
SELECT 3,'c'
UNION
SELECT 4,'d'
UNION
SELECT 5,'e'
UNION
SELECT 6,'f'
--SELECT * FROM dbo.sample
DECLARE @noOfRows int
set @noOfRows = 3
SELECT TOP @noOfRows * from dbo.sample -- Here it will through the rows
SELECT TOP (@noOfRows) * from dbo.sample -- This script will execute without any errors
Result:
Keywords:
select top dynamically,dynamically select top sql,t sql select top dynamically,SELECT TOP COUNT dynamically

0 comments:
Post a Comment