关于SQLServer的小技巧
exec sp_executesql @sql,N'@row_num int output', @row_num output
exec sp_executesql (参数1:动态SQL语句),(参数2:参数声明多个参数用逗号分开), 参数值
如下:
declare @sqlStr as nvarchar(1000)
set @sqlStr='set @s=3 select @s select @s1'
declare @outP as int
EXECUTE sp_executesql @sqlStr,
N'@s int output, @s1 int',
@outP output,1
select @outP
从SQLServer导出数据到Excel(按此原理应该也可以导出到ACCESS,或任何数据库。不过我没试~~)
insert into 表 select * from
OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 5.0;HDR=YES;DATABASE=c:\test.xls',sheet1$)
insert into importTest
select * from OPENROWSET
('MICROSOFT.JET.OLEDB.4.0' ,'Data Source=c:\storePlace.xls;Extended Properties=Excel 5.0',sheet1$)
--//这个正确!identity_insert
set identity_insert importTest on --让自动增长的列可以写入。
insert importTest (libID,libName,libMgr,flag)
select * from OPENROWSET
('MICROSOFT.JET.OLEDB.4.0' ,'Excel 5.0;HDR=YES;DATABASE=c:\storePlace.xls',sheet1$)
set identity_insert importTest off
--//这个不正确!identity_insert
set identity_insert importTest on --让自动增长的列可以写入。
insert into importTest --这里应该用列列表(libID,libName,libMgr,flag)
select * from OPENROWSET
('MICROSOFT.JET.OLEDB.4.0' ,'Excel 5.0;HDR=YES;DATABASE=c:\storePlace.xls',sheet1$)
set identity_insert importTest off
加上access的
--在SQLServer 中查询、操作Access数据库
--OpenDataSource Data Source后的文件名“要带引号”
select b1.a1,b1.a2,b1.a3 from OpenDataSource
('Microsoft.Jet.OLEDB.4.0','Data Source="F:\atest.mdb";')...b as b1
--OPENROWSET database后的文件“”“”“不能“”“””带引号 select * from OPENROWSET
('MICROSOFT.JET.OLEDB.4.0',';database=F:\atest.mdb','select * from a')
--access数据库将b表的数据导出到a表并保证没有重复的。
--a表b表字段都是a1,a2,a3
insert into OpenRowSet('microsoft.jet.oledb.4.0',';database=F:\atest.mdb','select * from a') (a1,a2,a3)
select b1.a1,b1.a2,b1.a3 from OpenDataSource
('Microsoft.Jet.OLEDB.4.0','Data Source="F:\atest.mdb";')...b as b1
where id not in (select b.ID from OpenDataSource
('Microsoft.Jet.OLEDB.4.0','Data Source="F:\atest.mdb";')...b as b inner join OpenDataSource
('Microsoft.Jet.OLEDB.4.0','Data Source="F:\atest.mdb";')...a as a on a.a1=b.a1 and a.a2=b.a2 and a.a3=b.a3)