SQL2000 how to use order by different conditions in stored procedures.

Updated on technology 2024-02-09
12 answers
  1. Anonymous users2024-02-06

    It is recommended that the @orderby int parameter be set to varchar, i.e., pass in the list of fields to be sorted directly.

    If it is empty, take addtime desc

    The stored procedure can be changed to look like this.

    declare @sql varchar(8000)set @sql='select * from table '

    if @orderby=''

    set @sql=@sql+' order by price1 asc '

    elseset @sql=@sql+' order by '+@orderby

    exec (@sql)

  2. Anonymous users2024-02-05

    In SQL, where and order by are used together to sort the data rows filtered by the query conditions specified by the where clause and the conditions specified by order by one or more columnsAscendingSort.

    Structured Query Language (SQL), or SQL for short, is a special-purpose programming language that is a database query and programming language used to access data and query, update, and manage relational database systems.

    The where clause sets the query conditions to filter out unwanted data rows.

    Use the order by clause to sort the results returned by a query by one or more columns. The syntax format of the order by clause is:

    order by [,n]

    among othersascdenotes ascending order, forDefault value, desc is descending.

  3. Anonymous users2024-02-04

    where column 2 = 'condition 1' This is the filtered data first, and then order by 'condition 2'

    Finally, the first piece of data is taken.

    In fact, you can test it yourself, and you don't need to ask others at all.

  4. Anonymous users2024-02-03

    It's still the top1 that implements condition 1 and condition 2.

  5. Anonymous users2024-02-02

    Where is just filtering, order by is sorting.

  6. Anonymous users2024-02-01

    The first statement is wrong, select @pp=sum(money) from a wnere (time condition) finds the total amount of all departments.

    In fact, the two statements can be written together, as follows:

    select depname,@pp=sum(money) from a where (time condition) group by depname

  7. Anonymous users2024-01-31

    I really don't understand what you're talking about.,If you group sales by unit, just add a group by statement to the end.。

  8. Anonymous users2024-01-30

    Using dynamic statements is to use SQL string operations to make up the statement you want to run and execute it

    declare @sql cmd varchar(1000) -- defines the variable used to store the statement.

    set @sql_cmd='select * from tab_ '--Note that there is a space before the quotation marks, if you have a space in front of your where, then this space can be dispensed with, the purpose of the space is to prevent the table name from being concatenated with the word where to produce unrecognizable commands.

    set @sql_cmd=@sql_cmd + 'where id='+ id --id is the input parameter, the number, if the parameter is text, enclose it with two consecutive single quotes, because the two consecutive single quotes in the text represent an actual single quote. That's how it is written as set @sql cmd=@sql cmd +' where account=''' +@account+''''

    Here you can tinker with the value of the text @sql cmd, not to mention adding where, you can change it to update.

    select @sql cmd -- finished tinkering, output this text to see if it's right.

    exec(@sql cmd)--Execute this text, pay attention to the parentheses, and the effect is the same as if you were running the statement directly.

  9. Anonymous users2024-01-29

    The case statement is a function that can only be used in DML statements, such as select, update, and delete, so you can only use if in this case.

  10. Anonymous users2024-01-28

    In the storage process, the if statement is used to determine the condition.

    create procedure stu(@id int)asif @id = 1

    beginselect ..

    endelse if @id = 2

    beginselect ..

    endelse

    beginselect ..end

  11. Anonymous users2024-01-27

    A column is a set of data, and the table name is a

    id class

    Query how many people are in each class of Class 1 and Class 2, and arrange them in class order, select class, count(*)from a where class in(1,2) group by class order by class

  12. Anonymous users2024-01-26

    It's useless for you to write like this on the outside, you can only write on the inside.

    For example, add a judgment to the stored procedure you execute.

    if day(getdate())20

    Begin executes certain contents.

    endelse

    Begin is not executed and the stored procedure is skipped.

    end is to give you an example, and you can write it yourself with reference to the actual situation.

Related questions
9 answers2024-02-09

Add the field status to varchar(10) in the database table

update database table name set status = from database table name inner join temporary table name b on database table name. qh = and database table name. tel = >>>More

5 answers2024-02-09

With a view or an index.

create index >>>More

5 answers2024-02-09

F get domain contrast is a formatting function (should be written by yourself), the first parameter (arg prod spec id) according to the second parameter (120) formatting and return the result to the v bill code, according to the function naming method, the above ** should be the standard format for getting the date.

4 answers2024-02-09

sele date,a.win, b, lose from table, (sele date count(result) as win from table where rsult='Win'group by date )a,,(sele date count(result) as pay from table where rsult='Pay'group by date )b where table. date=table. >>>More

4 answers2024-02-09

dateadd(dd, datediff(dd,0,getdate())0) means to return the current date. >>>More