更新时间:2023-01-16 08:05:52
尝试这个:创建 程序 MyProc
@ Column_Name VARCHAR ( 30 ),
@ Sort_Type VARCHAR ( 10 )
AS
BEGIN
DECLARE @sql VARCHAR ( 2000 )
SET @sql = ' SELECT *' +
' FROM Students' +
' ORDER BY' + @ Column_Name + ' ' + @ Sort_Type
EXEC ( @ sql )
END
govardhan4u [ ^ ]写道,请参阅本文:在存储过程中构建动态SQL [ ^ ]
Hey Guys,
i have a stored procedure which selects from the data base,
i want to add order by column as a parameter
and i want to add the sort type as a parameter too
EX: select * from Students order by @Column_Name @Sort_Type
@Column_Name is the sortable column
@Sort_Type is ASC or DESC
Any Ideas
Try this:CREATE PROCEDURE MyProc @Column_Name VARCHAR(30), @Sort_Type VARCHAR(10) AS BEGIN DECLARE @sql VARCHAR(2000) SET @sql = 'SELECT * ' + 'FROM Students ' + 'ORDER BY ' + @Column_Name + ' ' + @Sort_Type EXEC (@sql) END
As govardhan4u[^] wrote, see this article: Building Dynamic SQL In a Stored Procedure[^]