写在前面
在看同事写的代码时看到了SQL Server中可以自定义数据类型,而且定义的是DataTable类型的数据类型。 后我想起了以前我们导入数据时要么是循环insert写入,要么是SqlBulkCopy。 如果再去做这个导入时可以自定义DataTable数据类型,在存储过程里传入DataTable,直接在存储过程里使用insert from来实现。
T-SQL查询自定义数据类型
SELECT * FROM sys.types where is_user_defined=1
SQL Server中查看自定义数据类型
创建自定义数据类型语法
-- ================================ -- Create User-defined Table Type -- ================================ USE <database_name,sysname,AdventureWorks> GO -- Create the data type CREATE TYPE <schema_name,sysname,dbo>.<type_name,sysname,TVP> AS TABLE ( <columns_in_primary_key, , c1> <column1_datatype, , int> <column1_nullability,, NOT NULL>, <column2_name, sysname, c2> <column2_datatype, , char(10)> <column2_nullability,, NULL>, <column3_name, sysname, c3> <column3_datatype, , datetime> <column3_nullability,, NULL>, PRIMARY KEY (<columns_in_primary_key, , c1>) ) GO
自定义数据类型Demo
CREATE TYPE [dbo].[StudentTableType] AS TABLE( [id] [uniqueidentifier] NOT NULL, [name] [nvarchar](50) NULL, [Alias] [nvarchar](50) NULL, [Code] [nvarchar](50) NULL, [CreateDate] [datetime] NULL, [ModifiedDate] [datetime] NULL, [CreateBy] [int] NULL, [ModifyBy] [int] NULL ) GO