When you're working on a project, you want to do a random sort of table, which is exactly what you can say:
Declare a @parameter in the stored procedure, then the sort after using the query condition, according to my @parameter to implement, and I this @parameter actually pass in is a field value.
Google on the internet, found that there are such examples, most of the TI tree is a multiple-field of any paging stored procedures together, looking very complicated, and I do not need to make paging, no way to achieve it???
I first made one of these:
select * from SiteDetailInfo where SiteBasicId = SiteId And SiteIsCheck = 1 order by @Parameter desc
To run an error in Query Analyzer:
The%1! identified by the ORDER BY clause A SELECT item contains a variable that is in an expression at the location of the identity column. Variables are allowed in an expression only if they are referenced by a column name.
As if there is a grammatical problem, it is expected, vaguely remember should add a single quote, and so on, asked the netizen Stephen, soon lost the answer came, sure enough is a little grammatical problems.
The amendments are as follows:
create procedure proc_test
@fieldname varchar(50)
as
declare @sql varchar(1000)
set @sql= "update [table] set " + @fieldname + "= " + @fieldname + "+1 "
exec(@sql)
go
Or:
declare @field varcahr(10)
update table (case @field when "字段1名 " then set 字段1 = 字段1 + 1
when "字段2名 " then set 字段2 = 字段2 + 1
.... end)