sql 批量插入和升级

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 批量插入和升级相关的知识,希望对你有一定的参考价值。

USE [archive]
GO

/****** Object:  UserDefinedTableType [dbo].[mdCvLibraryType]    Script Date: 3/21/2017 9:45:16 AM ******/
DROP TYPE [dbo].[mdCvLibraryType]
GO

/****** Object:  UserDefinedTableType [dbo].[mdCvLibraryType]    Script Date: 3/21/2017 9:45:17 AM ******/
CREATE TYPE [dbo].[mdCvLibraryType] AS TABLE(
	[JobRef] [int] NOT NULL,
	[Date] [datetime] NULL,
	[Title] [nvarchar](100) NULL,
	[Company] [nvarchar](100) NULL,
	[Email] [nvarchar](100) NULL,
	[Url] [nvarchar](1000) NULL,
	[SalaryMin] [nvarchar](50) NULL,
	[SalaryMax] [nvarchar](50) NULL,
	[Benefits] [nvarchar](500) NULL,
	[Salary] [nvarchar](100) NULL,
	[JobType] [nvarchar](100) NULL,
	[Full_Part] [nvarchar](100) NULL,
	[Salary_Per] [nvarchar](100) NULL,
	[Location] [nvarchar](500) NULL,
	[Country] [nvarchar](100) NULL,
	[Description] [nvarchar](max) NULL,
	[Category] [nvarchar](500) NULL,
	[Image] [nvarchar](1000) NULL
)
GO


USE [staging]
GO

/****** Object:  StoredProcedure [dbo].[mdCvLibraryBulkAdd]    Script Date: 3/21/2017 9:46:42 AM ******/
DROP PROCEDURE [dbo].[mdCvLibraryBulkAdd]
GO

/****** Object:  StoredProcedure [dbo].[mdCvLibraryBulkAdd]    Script Date: 3/21/2017 9:46:42 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

--more info
-- https://www.aspsnippets.com/Articles/SqlBulkCopy--Bulk-Insert-records-and-Update-existing-rows-if-record-exists-using-C-and-VBNet.aspx



-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[mdCvLibraryBulkAdd]
  
	 @CvlJobList mdCvLibraryType READONLY

AS
BEGIN
	
	 SET NOCOUNT ON;	

	 MERGE INTO mdCvLibrary as cvl
	 USING @CvlJobList as jobList
	 ON cvl.JobRef = jobList.JobRef

	 WHEN MATCHED THEN
		UPDATE set 
				cvl.Title = jobList.Title,
				cvl.SalaryMin = jobList.SalaryMin,
				cvl.SalaryMax = jobList.SalaryMax,
				cvl.Benefits = jobList.Benefits,
				cvl.Salary = jobList.Salary,
				cvl.CvDescription = jobList.Description,
				cvl.ImageUrl = jobList.Image,
				cvl.Updated = GETDATE()
				
	 WHEN NOT MATCHED THEN
		INSERT VALUES (jobList.JobRef, 
		jobList.Date, 
		jobList.Title, 
		jobList.Company, 
		jobList.Email, 
		jobList.Url, 
		jobList.SalaryMin, 
		jobList.SalaryMax, 
		jobList.Benefits, 
		jobList.Salary, 
		jobList.JobType, 
		jobList.Full_Part, 
		jobList.Salary_Per, 
		jobList.Location, 
		jobList.Country, 
		jobList.Description, 
		jobList.Category, 
		jobList.Image, 
		GETDATE(),
		NULL);
END

GO

以上是关于sql 批量插入和升级的主要内容,如果未能解决你的问题,请参考以下文章

多线程海量删除查询和批量插入批量的 SQL 死锁

使用“一条”SQL语句完成批量插入和批量删除操作

使用“一条”SQL语句完成批量插入和批量删除操作

使用 LINQ to SQL 批量插入和复制记录

仅使用 REST 和 SQL 命令批量插入到 Snowflake

Myeclipse 如何用java 写sqlserver 的批量插入?