Home > Database > Mysql Tutorial > SQL 语句select top 变量问题

SQL 语句select top 变量问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 16:21:22
Original
1174 people have browsed it

今天我在写一个存储过程的时候,想动态的取若干行出来,开始用拼接字符串也可以,但是总觉得不好,就在网上查资料,找到一种方式,但是怎么也没有成功,后来终于发现是该方法不能在SQL2000中使用,只能在SQL2005以上使用。 1、拼接查询语句(SQL2000,2005,2

   今天我在写一个存储过程的时候,想动态的取若干行出来,开始用拼接字符串也可以,但是总觉得不好,,就在网上查资料,找到一种方式,但是怎么也没有成功,后来终于发现是该方法不能在SQL2000中使用,只能在SQL2005以上使用。

  1、拼接查询语句(SQL2000,2005,2008均可)

  DECLARE @a AS INT

  SET @a=1

  EXEC('SELECT TOP '+@a+' * FROM mtrcLanguages')

  2、直接使用(适用SQL2005以上)

  DECLARE @a AS INT

  SET @a=2

  SELECT TOP(@a) * FROM Common.tblCode tc

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Laravel doesn't read POST request with FormData
From 1970-01-01 08:00:00
0
0
0
php - How to optimize this sql?
From 1970-01-01 08:00:00
0
0
0
Error location, SQL statement
From 1970-01-01 08:00:00
0
0
0
node.js - gulp.task async issue
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template