EXCEL 导入SQL SERVER存储过程
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER proc [dbo].[pr_xls_to_tb]
@path varchar(200),--EXCEL路径名
@tbName varchar(30),--表名
@stName varchar(30) --excel中要读的SHEET名
as
declare @sql varchar(500),--最后要执行的SQL
@stName_Real varchar(35),--真正的SHEET名
@drop_sql varchar(300) -- 如果表已存在,先删除
set @stName_Real = '[' + @stName + '$]'
--set @path = 'C:\Inetpub\wwwroot\CarStock_ExcelWeb\Upload\CarStock\国贸汽车库存表20090630.xls'
--set @tbName = 't32'
--set @stName = '[不良资产$]'
set @sql =
'SELECT *
into '+ @tbName +'
from OpenDataSource(' + char(39)+ 'Microsoft.Jet.OLEDB.4.0' + char(39)+', '
+ char(39) +'Data Source=' + @path +';User ID=Admin;Password=;Extended properties=Excel 5.0;' + char(39)+')...'+@stName_Real
set @drop_sql = '
if exists(select * from sysobjects where name = ' + char(39) +@tbName + char(39)+')
begin
drop table '+@tbName+'
end '
--print @drop_sql
exec (@drop_sql)--先删除表
exec (@sql)--再创建表
/*
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
*/
调用:
exec pr_xls_to_tb 'C:\Tools\预算数据\ys_200908.xls' ,'ys_200908_new' ,'Source'
相关文档:
Group functions
SELECT [column,] group_function(column) ... from table [WHERE condition] [GROUP BY group_by_expression] [ORDER BY column];
e.g.:
SELECT department_id, job_id, SUM(salary), COUNT(employee_id) from employees GROUP BY department_id, job_id ;
SELECT [column,] group_function(column).. ......
oracle tips
Exist的用法:
select gw.ndocid from
(select ndocid from wf_doc_gw_shouwen union select ndocid from wf_doc_gw_fawen) gw
where
not exists (select null from wf_doc_gw_sn sn where sn.ndocid=gw.ndocid)
2。把GW表和SN表里相同的NDOCID显示出来
select gw.ndocid from
(se ......
原理:对需要去重复记录的字段按组排序,然后取其中一条记录。在总查询语句中使用in语法过滤
去掉重复记录
select * from company where comid in (select Max(comid) from company group by companyname)
得到重复记录数
select * from company where comid not in (select Max(comid) from company group by companyn ......
首先要添加
using System.Data;
using System.Data.SqlClient;
接下来:
SqlConnection conn = new SqlConnection("server=QLPC\\SQL2005;uid=sa;pwd=(你的密码);database=(你的数据表)"); &n ......
1 :普通SQL语句可以用exec执行
Select * from tableName
exec('select * from tableName')
exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N
2:字段名,表名,数据库名之类作为变量时,必须用动态SQL
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname fr ......