Home Database Mysql Tutorial SQL Server 应用开发 --- SQL SERVER 2000 数据查询综合实例

SQL Server 应用开发 --- SQL SERVER 2000 数据查询综合实例

Jun 07, 2016 pm 03:09 PM
200 server sql application development

SQL SERVER 2000 数据 查询 综合 实例 实例 1:更新用户卡信息 1、描述: 某公司印了一批充值卡,卡的密码是随机生成的,现在出现这个问题:卡里的"O"和"0","i"和"1",用户反映说看不清楚,公司决定,把存储在 数据 库中的密码中所有的"O"都改成"0",把所有

SQL SERVER 2000 数据查询综合实例
实例1:更新用户卡信息
  1、描述:
         某公司印了一批充值卡,卡的密码是随机生成的,现在出现这个问题:卡里的"O"和"0","i"和"1",用户反映说看不清楚,公司决定,把存储在数据库中的密码中所有的"O"都改成"0",把所有的"i"都改成"1"。
  2、实现:
     declare @Card table ([password] varchar(8)) -- 创建数据
     insert into @Card
     select 'abcdefgh' union
     select 'ijklmnop' union
     select 'qrstuvwx'
     select * from @Card
     update @Card set [password] = replace(replace([password],'o','0'),'i','1')
     select * from @Card
     go

实例2:特殊排序
  1、描述:
         在数据库表中有以下字符数据,如: 13-1、13-2、13-3、13-4、13-100、13-108、13-18、13-11、13-15、14-1、14-2, 现在希望通过SQL语句进行排序,并道德要按前半部份数字进行排序,然后再按后半部分的数字进行排序,输出要排成这样: 13-1、13-2、13-3、13-10、13-11、13-15、13-18、13-100、13-108、14-1、14-2
  2、实现:
     declare @SellRecord table (listNumber varchar(10)) -- 创建数据
     insert into @SellRecord
     select '13-1' union
     select '13-2' union
     select '13-3' union
     select '13-4' union
     select '13-100' union
     select '13-108' union
     select '13-18' union
     select '13-11' union
     select '13-15' union
     select '14-1' union
     select '14-2'
     select * from @SellRecord
     select * from @SellRecord order by convert(int,left(listNumber,charindex('-',listNumber)-1)),convert(int,stuff(listNumber,1,charindex('-',listNumber),' '))

实例3:查询一张表中的奇数行和偶数行
  1、描述:
         某单位中要根据奇数行和偶数行的数据来汇总,并在这个汇总的基础上再得到一个数值
  2、实现:
     -- 创建数据
     use pubs
     go
     if exists (select * from sysobjects where name = 'tbl') drop table tbl
     go
     create table tbl
     (
      idKey int identity(1,1) not null,
      a int
     )
     go
     insert into tbl (a) values (1)
     insert into tbl (a) values (2)
     insert into tbl (a) values (3)
     insert into tbl (a) values (4)
     insert into tbl (a) values (5)
     delete from tbl where idKey = 2
     go
     select * from tbl
     go
     -- 进行查询
     select identity(int,1,1) as [id], a
     into tempTbl
     from tbl
     go
     select * from tempTbl
     select sum(a) from tempTbl where [id]%2 != 0
     select sum(a) from tempTbl where [id]%2 = 0
     go

实例4:银行卡恢复
  1、描述:          一家银行发行了新的信用卡,刚开始的时候推广很好。但是逐渐地废卡越来越多,卡上的余额少于2元,并且用户长时间不使用该卡,因此银行在二月份把这些少于2元的卡的用户信息备份后就都从数据库表中删除了,但是很快问题就来了,用户发现他的卡再也不能使用而投拆,因此只能再把这些卡恢复。
  2、实现:
     use pubs
     go
     if exists (select * from sysobjects where name = 'S') drop table S
     go
     if exists (select * from sysobjects where name = 'M') drop table M
     go
     create table M
     (
       CardID int primary key not null,
       UserName varchar(20) not null
     )
     go
     create table S
     (
       CountID int identity(1,1) primary key, -- 帐户ID
       CardID int foreign key references M (CardID), -- 卡号
       Score float -- 余额
     )
     go
     insert into M (CardID,UserName) values (16,'张三')
     insert into M (CardID,UserName) values (23,'李四')
     insert into M (CardID,UserName) values (25,'王五')
     insert into M (CardID,UserName) values (29,'刘六')
     insert into M (CardID,UserName) values (30,'杨七')
     insert into S (CardID,Score) values (16,34.5)
     insert into S (CardID,Score) values (25,300)
     insert into S (CardID,Score) values (29,1.5)
     go
     select * from M
     select * from S
     go
     -- 恢复
     insert into S (CardID,Score) select M.CardID,2 from M left join S on M.CardID = S.CardID where S.CardID is null
     go
     select * from S
     go

实例5:
  1、描述:
         有如下二个表,将其中的数据进行合并,并按照学号进行分组,求出总分与平均分
  2、实现:
     -- 创建数据
     use pubs
     go
     if exists (select * from sysobjects where name = 'A') drop table A
     go
     if exists (select * from sysobjects where name = 'B') drop table B
     go
     create table A -- 数学成绩表
     (
       [id] int primary key,
       score int
     )
     go
     create table B -- 语言成绩表
     (
       [id] int primary key,
       score int
     )
     go
     insert into A values (16,66)
     insert into A values (23,56)
     insert into A values (25,67)
     insert into A values (29,45)
     insert into B values (23,80)
     insert into B values (25,90)
     insert into B values (29,59)
     insert into B values (30,84)
     go
     select * from A
     select * from B
     go
     -- 建立一个临时表并数据进行合并,并进行相关操作
     if exists (select * from sysobjects where name = 'C') drop table C
     go
     create table C -- 数学成绩表
     (
       [id] int,
       score int
     )
     go
     insert into C (id,score)
     select A.id, A.score from A union
     select B.id, B.score from B
     go
     select id as 学号, sum(score) as 总分, avg(score) as 平均分 from C group by id
     go

实例6:
  1、描述:
         有表ABC,其中有字段A、B和C,并且都是字符数据,其中A列存储了从A到Z之间的单个字母,查询出A列中字符在A到P之间的所有数据
  2、实现:
     -- 创建数据
     use pubs
     go
     if exists (select * from sysobjects where name = 'ABC') drop table ABC
     go
     create table ABC ( id varchar(1) )
     go
     -- 以5个字符为例
     insert into ABC values ('A')
     insert into ABC values ('B')
     insert into ABC values ('C')
     insert into ABC values ('D')
     insert into ABC values ('E')
     go
     select * from ABC where id between 'A' and 'C'
     go

实例7:
  1、描述:
         有学生成绩表,数据如下,查询出每门课都大于80分的学生姓名
  2、实现:
     -- 创建数据
     use pubs
     go
     if exists (select * from sysobjects where name = 'tb') drop table tb
     go
     create table tb ( stuName varchar(20), course varchar(20), score int )
     go
     insert into tb values ('张千','语文',80)
     insert into tb values ('张千','数学',77)
     insert into tb values ('李万','语文',66)
     insert into tb values ('李万','数学',91)
     insert into tb values ('王亿','语文',84)
     insert into tb values ('王亿','数学',100)
     insert into tb values ('王亿','英语',90)
     insert into tb values ('杨兆','英语',86)
     insert into tb values ('杨兆','数学',93)
     go
     select * from tb
     go
     -- 查询数据
     select stuName from tb group by stuName having min(score) >= 80
     go

实例8:
  1、描述:
         合并用户表,有3个表GameWOW,GameDiablo,GameStarCraft结构如下,将这三个表中的数据合并到新表Game中,新表结构如下,对于新表中存在而源表中不存在的记录,用NULL表示
  2、实现:
     use pubs
     go
     if exists (select * from sysobjects where name = 'GameWOW') drop table GameWOW
     go
     if exists (select * from sysobjects where name = 'GameDiablo') drop table GameDiablo
     go
     if exists (select * from sysobjects where name = 'GameStarCraft') drop table GameStarCraft
     go
     if exists (select * from sysobjects where name = 'Game') drop table Game
     go
     create table GameWOW
     (
       SName varchar(10),
       SPassWord varchar(10),
       SBirthday smalldatetime,
       SAddress varchar(10),
       SEmail varchar(10)
     )
     go
     insert into GameWOW values ('wow','wow',getdate(),'wow','wow')
     go
     create table GameDiablo
     (
       SName varchar(10),
       SPassWord varchar(10),
       SBirthday smalldatetime,
       SSex bit,
       SCardNumber varchar(10)
     )
     go
     insert into GameDiablo values ('diablo','diablo',getdate(),1,'diablo')
     go
     create table GameStarCraft
     (
       SName varchar(10),
       SPassWord varchar(10),
       SBirthday smalldatetime,
       SArea varchar(10),
       SCode int
     )
     go
     insert into GameStarCraft values ('starcraft','starcraft',getdate(),'starcraft',1)
     go
     create table Game
     (
       SName varchar(10),
       SPassWord varchar(10),
       SBirthday smalldatetime,
       SAddress varchar(10),
       SEmail varchar(10),
       SSex bit,
       SCardNumber varchar(10),
        SArea varchar(10),
       SCode int
     )
     go
     -- 合并
     insert into Game (SName,SPassWord,SBirthday,SAddress,SEmail,SSex,SCardNumber,SArea,SCode)
     select SName,SPassWord,SBirthday,SAddress,SEmail,null,null,null,null
     from GameWOW union
     select SName,SPassWord,SBirthday,null,null,SSex,SCardNumber,null,null
     from GameDiablo union
     select SName,SPassWord,SBirthday,null,null,null,null,SArea,SCode
     from GameStarCraft
     go
     select * from Game

实例9:
  1、描述:
         在论坛中采用一定的格式为主帖进行编号,格式为:版块编号_当前日期_四位随机数字
  2、实现:
     select 主贴编号 = '版块编号_' + convert(varchar(4),datepart(yyyy,getdate())) +
convert(varchar(2),datepart(mm,datepart(mm,getdate()))) +
convert(varchar(2),datepart(dd,datepart(dd,getdate()))) +
convert(varchar(4),right(rand(datepart(ms,getdate())*1000),4))

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Usage of division operation in Oracle SQL Usage of division operation in Oracle SQL Mar 10, 2024 pm 03:06 PM

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

Comparison and differences of SQL syntax between Oracle and DB2 Comparison and differences of SQL syntax between Oracle and DB2 Mar 11, 2024 pm 12:09 PM

Oracle and DB2 are two commonly used relational database management systems, each of which has its own unique SQL syntax and characteristics. This article will compare and differ between the SQL syntax of Oracle and DB2, and provide specific code examples. Database connection In Oracle, use the following statement to connect to the database: CONNECTusername/password@database. In DB2, the statement to connect to the database is as follows: CONNECTTOdataba

What does the identity attribute in SQL mean? What does the identity attribute in SQL mean? Feb 19, 2024 am 11:24 AM

What is Identity in SQL? Specific code examples are needed. In SQL, Identity is a special data type used to generate auto-incrementing numbers. It is often used to uniquely identify each row of data in a table. The Identity column is often used in conjunction with the primary key column to ensure that each record has a unique identifier. This article will detail how to use Identity and some practical code examples. The basic way to use Identity is to use Identit when creating a table.

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

Yunshen releases industry application flagship robot dog Jueying X30 Yunshen releases industry application flagship robot dog Jueying X30 Oct 11, 2023 pm 09:45 PM

On October 9, Yunshen Technology released the "Jueying X30" quadruped robot. As a new generation of industry-level products for industry applications, it is targeted at power stations, factories, pipe gallery inspections, emergency rescue, fire investigation, future scientific research, etc. Multi-field core demands bring the world's leading industry capabilities: original integrated sensing capabilities, taking the lead in Asia to achieve rapid and stable obstacle crossing in changing environments, up and down hollow industrial stairs, and all-weather autonomous inspections day and night, breaking more scene restrictions, It can respond quickly to unexpected tasks; for the first time in Asia, the operating temperature range of a quadruped robot has been extended to -20°C to 55°C, significantly broadening the application areas and seasons; it has its own real-time monitoring system and emergency response system to ensure smarter operations. Safe and efficient. Seventeen departments including the Ministry of Industry and Information Technology issued the "Machine

How to solve the 5120 error in SQL How to solve the 5120 error in SQL Mar 06, 2024 pm 04:33 PM

Solution: 1. Check whether the logged-in user has sufficient permissions to access or operate the database, and ensure that the user has the correct permissions; 2. Check whether the account of the SQL Server service has permission to access the specified file or folder, and ensure that the account Have sufficient permissions to read and write the file or folder; 3. Check whether the specified database file has been opened or locked by other processes, try to close or release the file, and rerun the query; 4. Try as administrator Run Management Studio as etc.

How to install, uninstall, and reset Windows server backup How to install, uninstall, and reset Windows server backup Mar 06, 2024 am 10:37 AM

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

See all articles