实现这一目标的最佳方式是什么
INSERT INTO @TableName (@ColumnNames)
EXEC sp_executesql @SQLResult;
@TableName
,@ColumnNames
,@SQLResult
为varchar
变量的位置
我试图避免为每个表单独插入。
答案 0 :(得分:1)
最好的方法是为所有表编写(或生成)所有reqiured过程。与所谓的“通用解决方案”相比,23个表时间可以自动生成的4个过程(插入,更新,删除和选择)没有任何开发时间和痛苦。
这是一条通向性能差,代码难以辨认,sql注入危险和无数次调试时间的途径。
答案 1 :(得分:0)
首先,我感谢您的所有评论。我同意SQL动态是一个痛苦的调试(感谢上帝,管理工作室有这种可能性)。当然,有数百种不同的解决方案
我终于以这种方式解决了它,或多或少我试着解释为什么这个SQL动态解决方案。客户端使用xlsx电子表格输入某些数据,因此我阅读电子表格并插入(数据取决于电子表格以插入到正确的表格中)。稍后,表中的数据将导出为XML以发送第三方软件。SET @SEL = N'';
DECLARE sel_cursor CURSOR
FOR (SELECT sc.name as field
FROM sys.objects so INNER JOIN sys.columns sc ON so.[object_id]=sc.[object_id]
WHERE so.name= @TableName and sc.name not in ('InitDate', 'EndDate', 'Version', 'Status'));
SET @SEL = ''; set @i = 0;
OPEN sel_cursor
FETCH NEXT FROM sel_cursor INTO @field
WHILE @@FETCH_STATUS = 0
BEGIN
set @sel = @sel + ', '+ @field
set @i = 1;
FETCH NEXT FROM sel_cursor INTO @field
END
CLOSE sel_cursor;
DEALLOCATE sel_cursor;
SET @SQL = N''
SET @SQL = @SQL + N'SELECT * INTO XLImport FROM OPENROWSET'
SET @SQL = @SQL + N'('
SET @SQL = @SQL + N'''Microsoft.ACE.OLEDB.12.0'''+','
SET @SQL = @SQL + N'''Excel 12.0 Xml; HDR=YES;'
SET @SQL = @SQL + N'Database='+@file +''''+ ','
SET @SQL = @SQL + N'''select * from ['+ @SheetName + '$]'''+');'
EXEC sp_executesql @SQL
SET @SQL = N'';
SET @SQL = @SQL + N'
SELECT '+''''+CAST(@initDate AS VARCHAR(10))+'''' +', '+ ''''+CAST(@endDate AS VARCHAR(10))+''''
+ ', '+ CAST(@version AS VARCHAR(2)) +', ' +''''+@status+''''
+ @SEL
+' FROM DBO.XLImport '
DECLARE cols_cursor CURSOR
FOR (Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS where table_name = @tableName);
SET @SEL = ''; set @i = 0;
OPEN cols_cursor
FETCH NEXT FROM cols_cursor INTO @field
WHILE @@FETCH_STATUS = 0
BEGIN
set @sel = @sel + @field + ', '
set @i = 1;
FETCH NEXT FROM cols_cursor INTO @field
END
CLOSE cols_cursor;
DEALLOCATE cols_cursor;
SET @SEL = LEFT(@SEL, LEN(@SEL) - 1) -- remove last ,
SET @SQL = N''
SET @SQL = @SQL + N'SELECT * INTO XLImport FROM OPENROWSET'
SET @SQL = @SQL + N'('
SET @SQL = @SQL + N'''Microsoft.ACE.OLEDB.12.0'''+','
SET @SQL = @SQL + N'''Excel 12.0 Xml; HDR=YES;'
SET @SQL = @SQL + N'Database='+@file +''''+ ','
SET @SQL = @SQL + N'''select * from ['+ @SheetName + '$]'''+');'
EXEC sp_executesql @SQL
SET @SQLString =
N'INSERT INTO '+ @TableName + '('+ @SEL +') ' + @SQL;
EXEC sp_executesql @SQLString
答案 2 :(得分:0)
使用EXECUTE sp_executesql @sql
,例如:
create proc sp_DynamicExcuteStore
@TableName varchar(50),
@ColumnNames varchar(50),
@SQLResult varchar(max)
as
declare @sql nvarchar(max) = '
INSERT INTO '+@TableName+' ('+@ColumnNames+')
EXEC sp_executesql '+@SQLResult
EXECUTE sp_executesql @sql
go
create proc sp_test
as
select 'test' + convert(varchar,RAND())
go
CREATE TABLE [dbo].[Test](
[text1] [nvarchar](500) NULL
) ON [PRIMARY]
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[sp_DynamicExcuteStore]
@TableName = N'Test',
@ColumnNames = N'text1',
@SQLResult = N'proc_test'
SELECT 'Return Value' = @return_value
GO
SELECT TOP 1000 [text1]
FROM [test].[dbo].[Test]