SqlServer日常积累(一)
1. 将一个表的数据插入另一个表
情况一:目标表已存在
(1)如果2张表的字段一致,并且希望插入全部数据,可以用这种方法:
Insert Into 目标表 Select * From 来源表;
--例如
Insert Into newArticles Select * From articles;
(2)如果只希望导入指定字段,可以用这种方法:
Insert Into 目标表 (字段1, 字段2, ...) Select 字段1, 字段2, ... From 来源表;
情况二:目标表不存在
(1)如果将一个表的数据放在另外一个不存在的表:
Select * Into 目标不存在的表 From 来源表
(2)如果只希望导入指定字段,可以用这种方法:
Select 字段1,字段2,... Into 目标不存在的表 From 来源表
2. sql修改表的字段类型
alter table 表名 alter column 字段名 新的字段类型
--例如
alter table mytable alter column mycol1 int default 0
3. 远程导入表数据(不同服务器数据库之间的数据操作)
方法一:
(title,author,riqi,content,ispic,istop,pic,zhaiyao)
select Title,Author,UpdateTime,Content,IncludePic,OnTop,DefaultPicUrl,cast(lmd as varchar(20)) from Article
启用Ad Hoc Distributed Queries:
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
关闭Ad Hoc Distributed Queries:
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
5. 用第二个表数据更新主表字段
方式一(适用于多字段):
UPDATE tableA
SET name = B.name, age = B.age
FROM tableA A,tableB B WHERE A.Id = B.Id
方式二(适用于单字段):
UPDATE tableA
SET name = (SELECT B.name FROM tableB B
WHERE B.Id = Id)
6. 添加行号列(row_number()此函数2005后数据库可用)
select row_number()over(order by userid )as RowNum, * from tableName
7.动态语句中添加变量
(1)非字符串类型变量
declare
@Number int,@sql nvarchar(1000)
set @Number = 19;
set @sql = 'select Time, Humidity, EquipmentNum
from DataMonitoring
where Id = '+convert(nvarchar(10),@Number);
exec(@sql);
(2)字符串类型变量
declare
@Char nvarchar(20),@sql nvarchar(1000)
set @Char = '00:00';
set @sql = 'select Time, Humidity, EquipmentNum
from DataMonitoring
where Time = '''+@Char+'''';
exec(@sql);
8.选取数据插入临时表并添加自增行号
declare
@maxRowNum int, @sql nvarchar(1000)
select Time, Humidity, EquipmentNum, RowNum = identity(int,1,1)
into #temp0 from DataMonitoring where Time = '00:00' select @maxRowNum = max(RowNum) from #temp0; set @sql = 'select Time, Humidity, EquipmentNum, RowNum = identity(int,'+convert(nvarchar(10),@maxRowNum+1)+',1)
into ##temp0 from DataMonitoring where Time = ''01:00''';
exec(@sql);
select * from ##temp0 union select * from #temp0 order by RowNum;
drop table #temp0; drop table ##temp0;
9. @Result = @@ROWCOUNT
返回受上一语句影响的行数。 如果行数大于 20 亿,请使用 @@ROWCOUNT_BIG。
10. 在sql存储过程中,不能使用表明变量(如:@tableName),若想使用表明变量只可使用动态Sql语句。
Create Procedure GetList
@tablename varchar(20)
AS
BEGIN
declare @sqlcommand varchar(max) --sql名称串变量
set @sqlcommand ='select * from '+@tablename --构造这个字符串
exec(@sqlcommand ) --执行sql命令
END
GO
SqlServer日常积累(一)的更多相关文章
- SqlServer日常积累(三)
1.TRUNCATE 和 DELETE TRUNCATE操作没有记录删除操作日志 主要的原因是因为 TRUNCATE 操作不会激活触发器,因为TRUNCATE操作不会记录各行删除操作的日志,所以当你需 ...
- SqlServer日常积累(二)
1.Like运算符:将字符串表达式与 SQL表达式中的模式进行比较匹配. 语法 :expression Like 'pattern' ,expression为匹配字段,pattern为匹配字符串.可以 ...
- Python 日常积累
包管理 >from ... import ... 的用法和直接import的区别 直接使用import时,如果需要使用到导入模块内的属性和方法,必须使用模块名.属性和模块名.方法的方式进行调用 ...
- 日常积累oracle 有关信息
对于VARCHAR2类型,我们在内存使用和效率上需要做出一个权衡.对于VARCHAR2(长度>=2000)变量,PL/SQL动态分配内存来存放实际值,但对于VARCHAR2(长度<2000 ...
- 日常积累之JSON.stringify和JSON.parse substr
1.substr(start,len) 从字符串中读取内容,第一个参数是读取的首位置,如果为负数,则从末尾倒数计数. 第二个参数是要读取的长度. eg: var str = "silence ...
- 苹果浏览器和uc浏览器在移动端的坑(日常积累,随时更新)
先mark 1 . 移动端uc浏览器不兼容css3 calc() 2 . ie8下a标签没有内容给宽高也不能触发点击跳转 3 . safari输入框加上readOnly="ture&qu ...
- SQL 性能调优日常积累
我们要做到不但会写SQL,还要做到写出性能优良的SQL,以下为笔者学习.摘录.并汇总部分资料与大家分享! (1)选择最有效率的表名顺序(只在基于规则的优化器中有效) ORACLE 的解析器按照从右到左 ...
- SQL 性能调优日常积累【转】
阅读目录 (1)选择最有效率的表名顺序(只在基于规则的优化器中有效) (2)WHERE子句中的连接顺序 (3)SELECT子句中避免使用 ‘ * ‘ (4)减少访问数据库的次数 (5)在SQL*Plu ...
- sqlserver日常维护脚本
SQL code --备份declare @sql varchar(8000) set @sql='backup database mis to disk=''d:\databack\mis\mis' ...
随机推荐
- java基础学习总结——开篇
java是我学习的第一门编程语言,当初学习java基础的时候下了不少功夫,趁着这段时间找工作之际,好好整理一下以前学习java基础时记录的笔记,当作是对java基础学习的一个总结吧,将每一个java的 ...
- UILabel的缩放动画效果
UILabel的缩放动画效果 效果图 源码 https://github.com/YouXianMing/Animations // // ScaleLabel.h // Animations // ...
- Apache2.2.16+PHP5.3.3+MySQL5.1.49的配置方法
第一步:下载安装的文件 1. MySQL:下载地址mysql-5.1.49-win32.msi: 2. Apache: 下载地址httpd-2.2.16-win32-x86-openssl-0.9.8 ...
- Ladda 应用提交表单的时候显示loading载入中 包含不同位置,不同效果
Ladda 应用提交表单的时候显示loading载入中 包含不同位置,不同效果 不同大小.位置,效果,进度条等 演示 XML/HTML Code <article class="exa ...
- sscanf,sprintf用法
#include<string.h> #include<stdio.h> int main() { ],sztime1[],sztime2[]; sscanf("12 ...
- strstr实现
// strstr.c查找完全匹配的子字符串 #include<stdio.h> #include<string.h> char *my_strstr(const char * ...
- 机器学习算法之旅A Tour of Machine Learning Algorithms
In this post we take a tour of the most popular machine learning algorithms. It is useful to tour th ...
- coursera课程Text Retrieval and Search Engines之Week 2 Overview
Week 2 OverviewHelp Center Week 2 On this page: Instructional Activities Time Goals and Objectives K ...
- 【12c】root container 和 pdb 的一些差别
Where\what ? root pdb 备注 Control files and redo log files Y belongs to the CDB and not to a spec ...
- HorizontalScrollView的使用演示样例
MainActivity例如以下: package cc.cv; import android.os.Bundle; import android.view.LayoutInflater; impor ...