sqlserver 插入语句

//--创建事务
Create PROC [dbo].[proc_XXXXX]   
@xxx varchar(50)
AS
BEGIN
BEGIN TRAN
BEGIN TRY 
    .....................插入
    COMMIT TRAN 
END TRY

BEGIN CATCH
    SELECT ERROR_MESSAGE()
    ROLLBACK TRAN
END CATCH
END
GO
插入百万条数据
use test
DECLARE @i INT
set @i=0;
DECLARE @id INT
SET @id=0
WHILE @i<1000000      --100w为你要执行插入的次数
BEGIN
INSERT INTO Table_2(id)  --xx为表名
VALUES  ( @i+1) 
SET @i=@i+1
END
插入百万条数据2
DECLARE @LN VARCHAR(300),@MN VARCHAR(200),@FN VARCHAR(200)
DECLARE @LN_N INT,@MN_N INT,@FN_N INT
SET @LN='李王张刘陈杨黄赵周吴徐孙朱马胡郭林何高梁郑罗宋谢唐韩曹许邓萧冯曾程蔡彭潘袁于董余苏叶吕魏蒋田杜丁沈姜范江傅钟卢汪戴崔任陆廖姚方金邱夏谭韦贾邹石熊孟秦阎薛侯雷白龙段郝孔邵史毛常万顾赖武康贺严尹钱施牛洪龚'
SET @MN='德绍宗邦裕傅家積善昌世贻维孝友继绪定呈祥大正启仕执必定仲元魁家生先泽远永盛在人为任伐风树秀文光谨潭棰'
SET @FN='丽云峰磊亮宏红洪量良梁良粮靓七旗奇琪谋牟弭米密祢磊类蕾肋庆情清青兴幸星刑'
SET @LN_N=LEN(@LN)
SET @MN_N=LEN(@MN)
SET @FN_N=LEN(@FN)
DECLARE @TMP VARCHAR(1000),@I INT
SET @I=1
WHILE @I<=1000000
BEGIN
    SET @TMP=CAST(SUBSTRING(@LN,CAST(RAND()*@LN_N AS INT),1) AS VARCHAR)
    SET @TMP=@TMP+CAST(SUBSTRING(@MN,CAST(RAND()*@MN_N AS INT),1) AS VARCHAR)
    SET @TMP=@TMP+CAST(SUBSTRING(@FN,CAST(RAND()*@FN_N AS INT),1) AS VARCHAR)
    INSERT INTO student(sname)VALUES(@TMP)
    SET @I=@I+1
END
若存在插入
use test
insert into Table_1(A,B) 
select  'B','B'
where not exists(select * from Table_1 where A='B')  --先查询在插入 --返回受影响的行 
不知不觉分不开 2018-09-28 11:18:07

if not exists(select * from xxxss where xxx=1) 
begin
  ................. //插入语句
end
go 

 

INSERT INTO [dbo].[Lock]([lock], ax001) select  0 ,ISNULL(MAX(ax001),0)+1 from [lock]

 

posted @ 2019-11-25 11:50  enych  阅读(6890)  评论(0编辑  收藏  举报