我有100个列的宽表。
我需要一个SP,它接受100个参数然后插入。
我知道如何手动执行此操作。但是有了表定义并知道SP参数将具有与表列完全相同的名称,您能想到更好/更快的方式来生成此存储过程吗?
答案 0 :(得分:3)
我使用SQL为你编写它。检查一下,如果需要任何调整或者您有任何问题,请告诉我。
IF OBJECT_ID('yourTable') IS NOT NULL
DROP TABLE yourTable;
CREATE TABLE yourTable
(
col1 INT,
col2 VARCHAR(100),
col3 NUMERIC(18,2)
)
DECLARE @InputParams VARCHAR(MAX),
@InsertColumns VARCHAR(MAX),
@InsertParams VARCHAR(MAX);
WITH CTE_columns
AS
(
SELECT COLUMN_NAME,
UPPER(DATA_TYPE) data_type,
'(' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(10)) + ')' max_length,
CASE
WHEN DATA_TYPE IN ('Numeric','Decimal') THEN CONCAT('(',NUMERIC_PRECISION,',',NUMERIC_SCALE,')')
END prec_scale
--@InsertColumns = COALESCE(@InsertColumns + ',','') + COLUMN_NAME,
FROM INFORMATION_SCHEMA.COLUMNS A
WHERE TABLE_NAME = 'yourTable'
)
SELECT @InputParams = COALESCE(@InputParams + ',','') + CONCAT('@',column_name,' ',data_type,max_length,prec_scale),
@InsertColumns = COALESCE(@InsertColumns + ',','') + COLUMN_NAME,
@InsertParams = COALESCE(@InsertParams + ',','') + '@'+ COLUMN_NAME
FROM CTE_columns
SELECT
'CREATE PROCEDURE dbo.yourProc ' + @InputParams +
' AS
INSERT INTO yourTable(' + @InsertColumns + ')
VALUES (' + @InsertParams + ');
GO'
结果(格式化不是很好,但它有效):
CREATE PROCEDURE dbo.yourProc @col1 INT,@col2 VARCHAR(100),@col3 NUMERIC(18,2) AS
INSERT INTO yourTable(col1,col2,col3)
VALUES (@col1,@col2,@col3);
GO
答案 1 :(得分:2)
对于这种情况,我喜欢利用表格类型。
首先,创建表类型:
CREATE TYPE [YourType] AS TABLE (columns...)
在C#中,通过运行此查询(使用DataTable
)填充DataAdapter
“模板”:
DECLARE @tt AS [YourType]
SELECT * FROM @tt
然后在表格中添加一行并在列中循环以添加必要的值。
然后将数据表作为参数传递到您的过程中:
CREATE PROCEDURE [YourProc]
@tt [YourType] READONLY
AS
BEGIN
--do stuff
END
此方法的另一个好处是,您可以通过一次调用将多个记录传递到过程中。
答案 2 :(得分:1)
您可以根据需要快速修改它:
Declare @tableName nvarchar(100) = 'aspnet_Membership'
Declare @objectName nvarchar(100) = 'Membership'
DECLARE @newLineChar AS CHAR(2) = CHAR(13) + CHAR(10)
Declare @columnName nvarchar(50)
Declare @dataType nvarchar(50)
Declare @characterLength int
Declare @isNullableString varchar(3)
Declare @ordinalPosition int
Declare @firstSortOrder smallint
Declare @variableTableCreateCode nvarchar(max) = ''
Declare @variableTableDataTypeCode nvarchar(128)
Declare @variableTableSelectCode001 nvarchar(max) = ''
Declare @variableTableSelectCode002 nvarchar(max) = ''
Declare @varableTableInsertIntoCode nvarchar(max) = ''
Declare tbl_ColumnCursor Cursor For
Select colSchema.COLUMN_NAME, colSchema.DATA_TYPE, colSchema.CHARACTER_MAXIMUM_LENGTH, colSchema.IS_NULLABLE , colSchema.ORDINAL_POSITION , FirstSortOrder = CASE WHEN PrimaryKeyDerived.CONSTRAINT_TYPE IS NOT NULL THEN 1 ELSE 2 END
from INFORMATION_SCHEMA.COLUMNS colSchema
LEFT JOIN
(
SELECT
INFORMATION_SCHEMA.COLUMNS.TABLE_NAME
,INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME
,INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
LEFT OUTER JOIN information_schema.KEY_COLUMN_USAGE
ON
INFORMATION_SCHEMA.COLUMNS.TABLE_NAME=information_schema.KEY_COLUMN_USAGE.TABLE_NAME
AND INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME=information_schema.KEY_COLUMN_USAGE.COLUMN_NAME
LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS
ON
information_schema.KEY_COLUMN_USAGE.TABLE_NAME=information_schema.TABLE_CONSTRAINTS.TABLE_NAME
AND information_schema.KEY_COLUMN_USAGE.CONSTRAINT_NAME=information_schema.TABLE_CONSTRAINTS.CONSTRAINT_NAME
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
) as PrimaryKeyDerived on colSchema.TABLE_NAME = PrimaryKeyDerived.TABLE_NAME and colSchema.COLUMN_NAME = PrimaryKeyDerived.COLUMN_NAME
where colSchema.TABLE_NAME = @tableName
Order By FirstSortOrder , colSchema.ORDINAL_POSITION
Set @variableTableCreateCode = 'declare @' + @objectName + 'Holder table ( ' + @objectName + 'SurrogateKey int , '
Set @variableTableSelectCode001 = 'INSERT INTO @' + @objectName + 'Holder ( '
Set @variableTableSelectCode002 = ' SELECT '
Set @varableTableInsertIntoCode = 'INSERT INTO '+@tableName+' ( '
Open tbl_ColumnCursor
Fetch Next From tbl_ColumnCursor Into @ColumnName, @dataType, @characterLength, @isNullableString, @ordinalPosition , @firstSortOrder
While @@FETCH_STATUS = 0
Begin
print '/@ColumnName/'
print @ColumnName
print ''
print '/@@dataType/'
print @dataType
print ''
print '/@@@isNullableString/'
print @isNullableString
print ''
Select @variableTableDataTypeCode =
case
when @dataType like '%char%' and @characterLength = -1 Then
@dataType+'(max)'
when @dataType like '%char%' and @characterLength = 1 Then
@dataType+'('+convert(varchar(32), @characterLength)+')'
When (@dataType like '%char%') Or (@dataType = 'xml') Or (@dataType like '%text%') Then
@dataType+'('+convert(varchar(32), @characterLength)+')'
Else @dataType
End
Set @variableTableCreateCode = @variableTableCreateCode + ' [' + @columnName + '] ' + @variableTableDataTypeCode +','
select @variableTableSelectCode002 = @variableTableSelectCode002 + 'T.MyEntity.value(''@' + @columnName + ''', '''+@variableTableDataTypeCode+''') AS ' + @columnName + ','
Fetch Next From tbl_ColumnCursor Into @ColumnName, @dataType, @characterLength , @isNullableString , @ordinalPosition , @firstSortOrder
End
Close tbl_ColumnCursor
Deallocate tbl_ColumnCursor
if(DATALENGTH(@variableTableCreateCode) > 0)
BEGIN
select @variableTableCreateCode = LEFT(@variableTableCreateCode, LEN(@variableTableCreateCode) -1)
END
if(DATALENGTH(@variableTableSelectCode002) > 0)
BEGIN
select @variableTableSelectCode002 = LEFT(@variableTableSelectCode002, LEN(@variableTableSelectCode002) -1)
END
Set @variableTableCreateCode = @variableTableCreateCode + @newLineChar + ' ) '
select @variableTableCreateCode
select @variableTableSelectCode001 = @variableTableSelectCode001 + ( SELECT
MyColumns = STUFF
(
(
SELECT ', [' + r.COLUMN_NAME + ']'
FROM INFORMATION_SCHEMA.COLUMNS AS r
WHERE r.TABLE_NAME = tabs.TABLE_NAME
ORDER BY r.ORDINAL_POSITION
FOR XML PATH(''), TYPE
).value('.[1]','nvarchar(max)'),
1,1,''
)
FROM INFORMATION_SCHEMA.TABLES AS tabs
/* Optional WHERE Clause */
WHERE
tabs.TABLE_NAME = @tableName
)
select @variableTableSelectCode001 = @variableTableSelectCode001 + ' ) '+@newLineChar
select @variableTableSelectCode002 = @variableTableSelectCode002 + ' FROM @xmldata.nodes(''RootElement/' + @tableName + 'Elements/' + @tableName + 'Element'') AS T(MyEntity); '
select @variableTableSelectCode001 + @variableTableSelectCode002
select @varableTableInsertIntoCode = @varableTableInsertIntoCode + ( SELECT
MyColumns = STUFF
(
(
SELECT ', [' + r.COLUMN_NAME + ']'
FROM INFORMATION_SCHEMA.COLUMNS AS r
WHERE r.TABLE_NAME = tabs.TABLE_NAME
ORDER BY r.ORDINAL_POSITION
FOR XML PATH(''), TYPE
).value('.[1]','nvarchar(max)'),
1,1,''
)
FROM INFORMATION_SCHEMA.TABLES AS tabs
WHERE
tabs.TABLE_NAME = @tableName
)
select @varableTableInsertIntoCode = @varableTableInsertIntoCode + ' ) SELECT '
select @varableTableInsertIntoCode = @varableTableInsertIntoCode + ( SELECT
MyColumns = STUFF
(
(
SELECT ', holder.[' + r.COLUMN_NAME + ']'
FROM INFORMATION_SCHEMA.COLUMNS AS r
WHERE r.TABLE_NAME = tabs.TABLE_NAME
ORDER BY r.ORDINAL_POSITION
FOR XML PATH(''), TYPE
).value('.[1]','nvarchar(max)'),
1,1,''
)
FROM INFORMATION_SCHEMA.TABLES AS tabs
WHERE
tabs.TABLE_NAME = @tableName
)
select @varableTableInsertIntoCode = @varableTableInsertIntoCode + ' from @' + @objectName + 'Holder holder where not exists ( select null from ' +@tableName+ ' realTable where realTable.Name = holder.Name and realTable.IsDeleted = holder.IsDeleted ) '
select @varableTableInsertIntoCode
SELECT
tabs.TABLE_NAME,
MyColumns = STUFF
(
(
SELECT ', alias.[' + r.COLUMN_NAME + ']'
FROM INFORMATION_SCHEMA.COLUMNS AS r
WHERE r.TABLE_NAME = tabs.TABLE_NAME
ORDER BY r.ORDINAL_POSITION
FOR XML PATH(''), TYPE
).value('.[1]','nvarchar(max)'),
1,1,''
)
FROM INFORMATION_SCHEMA.TABLES AS tabs
/* Optional WHERE Clause */
WHERE
tabs.TABLE_NAME = @tableName
ORDER BY tabs.TABLE_NAME;