1、SQL语句中的输出字段用n ame varchar(10,value int你的sql应该这么写(用存储过程:create proc p_ins_al m_msgmsg_co unt int outasmsg_co untif rowco unt = 0return -1000in sert into table1 (field。values(msg_co untreturn 0go在sql server中如何使用sql语句修改表中的字段名称a.重命名表下例将表customers重命名为custsexec sp_re name customers, custsb.重命名列下例将表custome
2、rs中的列con tact title重命名为title。exec sp_re name customers.on tact title, title, colu mn怎么那么不喜欢查看联机帮助呢怎么用sql语句在sql2008中的表中添加数据字段而且不是空字段添加字段 :alert table name add字 段名属性添加带默认值的字段alert table name add字 段名属性 default 值例如:alert mytable add name varchar(20 default wangmi篇二:用 sql 语句表与字段的基本操作、数据库备份等用sql语句添加删除修改字段
3、1.增加字段alter table docdsp add dspcode char(2002.删除字段alter table table_ name drop colu mn colu mn_n ame3.修改字段类型alter table table_ name alter colu mn colu mn_n ame n ew_data_type4.sp_re name改名更改当前数据库中用户创建对象(如表、列或用户定义数据类型的名称。语法sp_re name objn ame = object_ name ,newn ame = new_n ame,objtype = object_typ
4、e 如:exec sp_re name newn ame,partstock5.sp_help显示表的一些基本情况sp_help object_ name如:exec sp_help partstock6.判断某一表partstock中字段partvelocity是否存在if exists (select * from syscolu mns where id=object_id(partstock andn ame=partvelocityprint partvelocity existselse print partvelocity not exists另法:判断表的存在性:select
5、coun t(* from sysobjects where type=u and n ame你的表名判断字段的存在 性:select coun t(* from syscolu mnswhere id = (select id from sysobjects where type=u and n ame你的表名and name = 你要判断的字段名一个小例子-假设要处理的表名为:tb-判断要添加列的表中是否有主键if exists(select 1 from sysobjects where pare nt_o bj=object_id(tb and xtype=pk begi nprint
6、表中已经有主键,列只能做为普通列添加-添加int类型的列,默认值为0alter table tb add 列名 int default 0endelsebegi nprint表中无主键,添加主键列-添加int类型的列,默认值为0alter table tb add 歹U名 int primary key default 0end7随机读取若干条记录access语法:select top 10 * from 表名 order by rnd(idsql server:select top n * from 表名 order by n ewid(mysql select * from 表名 orde
7、r by rand( limit n8.说明:日程安排提前五分钟提醒sql: select * from 日程安排 where datediff(minute,f 开始时间,getdate(5 9前 10 条记录select top 10 * form tablel where 范围10.包括所有在tablea中但不在tableb和tablec中的行并消除所有重复行而派生 出一个结果表(select a from tablea except (select a from tableb except (select a from tablec 说. 明:随机取出10条数据select top 1
8、0 * from table name order by n ewid(12.列出数据库里所有的表名select n ame from sysobjects where type=u13.列出表里的所有的字段名select n ame from syscolu mns where id=object_id(table name14.说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多 重选择,类似select中的caseselect type,sum(case ven der whe n a the n pcs else 0 en d,sum(case v
9、en der whe n c then pcs else 0 en d,sum(case ven der whe n b the n pcs else 0 end from table name group by type15.说明:初始化表table1trun cate table table116.说明:几个高级查询运算词a: union运算符union运算符通过组合其他两个结果表(例如tablel和table2并消去表中任何重 复行而派生出一个结果表。当 all随union起使用时(即union all,不消除重复行。 两种情况下,派生表的每一行不是来自tablel就是来自table2。
10、b: except运算符except运算符通过包括所有在tablel中但不在table2中的行并消除所有重复行 而派生出一个结果表。当all随except 一起使用时(except all,不消除重复行。c: in tersect 运算符in tersect运算符通过只包括tablel和table2中都有的行并消除所有重复行而派生出一个结果表。当all随in tersect 一起使用时(in tersect all不消除重复行。注:使用运算词的几个查询结果行必须是一致的。17.说明:在线视图查询(表名1:aselect * from (select a,b,c from a t where t
11、.a 1;18.说明:between的用法,between限制查询数据范围时包括了边界值,not between 不包括select * from tablel where time between timel and time2select a,b,c, from tablel where a not betwee数值 1 and数值 219.说明:in的使用方法select * from table1 where a not n (值 1值 2值 4值 6 2说明:两张关联 表,删除主表中已经在副表中没有的信息delete from table1 where not exists ( se
12、lect * from table2 where table1.field1=table2.field121.说明:复制表(只复制结构 源表名:a新表名:b (access可用法一:select * into b from a where 11法二:select top 0 * into b from a22.说明:拷贝表(拷贝数据,源表名:a目标表名:b (access可用in sert into b(a, b, c select d,e,f from b;23.说明:跨数据库之间表的拷贝(具体数据使用绝对路径(access可用insert into b(a, b, c select d,e
13、,f from b in 具体数据库 wh6条件例子:.from b in& server.mappath(. &data.mdb & where.24.创建数据库create database database-n ame25.说明:删除数据库drop database dbn ame26.说明:备份 sql server-创建备份数据的deviceuse masterexec sp_addumpdevice disk, testback, c:mssql7backupm ynwin d_1.dat-开始备份backup database pubs to testback27.说明:创建新表
14、create table tabname(col1 typel not null primary key,col2 type2 not null,.根据已有的表创建新表:a:create table tab_new like tab_old使用旧表创建新表b:create table tab_ new as select col1,col2 from tab_old defir说碉:only 28.删除新表:drop table tab name29.说明:增加一个歹U :alter table tab name add colu mn col type注:列增加后将不能删除。db2中列加上后
15、数据类型也不能改变,唯一能改变的 是增加varchar类型的长度。30.说明:添加主键:alter table tab name add primary key(col说明:删除主键:alter table tab name drop primary key(col31.说明:仓U建索弓丨:create unique index idxname on tabname(col删除索引:drop index idxname注:索引是不可更改的,想更改必须删除重新建。32.说明:仓 U建视图:create view view name as select stateme nt删除视图:drop view view name33.说明:几个简单的基本的sql语句选择:select * from tablel where 范围插入:insert into table1(field1,field2 values(value1,value2删除:delete from table1 where范 围
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1