数据同步存储过程

USE [TKPOS_WuJing]
GO
/****** Object: StoredProcedure [dbo].[sp_get_saleData] Script Date: 07/21/2014 11:20:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author: 陈新光
-- Create date: 2014-05-28
-- Description: 获取要上传的销售数据
/*
execute sp_get_saleData
*/
-- =============================================
/*
TSaleRecord = record // 销售数据
recNo: string[40]; // guid
shopid: string[40]; // 门店编号
shopname: string[40];
billId: string[40]; // 单据编号
saleDate: TDatetime; // 销售日期
goodsId: string[40]; // 商品编号
goodsName: string[40];
price: Currency; // 价格
qty: Single; // 数量
amount: Currency; // 金额
cookType: string[8]; // 班次
bigKindId: string[40]; // 大类编号
bigKindName: string[40];
smallKindId: string[40]; // 小类编号
smallKindName: string[40];
unitId: string[40]; // 计量单位编号
unitName: string[40];
vipId: string[40]; // 会员编号
vipName: string[40];
skyId: string[40]; // 收款员编号
skyName: string[40];
ywyId: string[40]; // 业务员编号
ywyName: string[40];
end;
*/
ALTER PROCEDURE [dbo].[sp_get_saleData]
--@bills varchar(400) output -- 将要上传的销售单号列表
AS
BEGIN
set nocount on
-- 获取最近上传数据的时间戳
declare @dd datetime
select @dd=LastUpload from sys_upload where UploadType='SaleData'
-- 将要上传哪些销售单
declare @billIds varchar(400)
declare @billId varchar(40)
DECLARE c1 CURSOR FOR
select top 5 SaleNo from Pos_Master where SaleDate>@dd -- 一次上传5笔
open c1
FETCH NEXT FROM c1 INTO @billId
WHILE @@FETCH_STATUS = 0
BEGIN
if @billIds is null begin
set @billIds=''''+@billId+''''
end else begin
set @billIds=@billIds+','+''''+@billId+''''
end
FETCH NEXT FROM c1 INTO @billId
end
close c1
deallocate c1

declare @sql varchar(2000)
declare @time2 datetime
create table #tmp(
time2 datetime
)
set @sql='insert into #tmp select max(SaleDate) from Pos_Master where SaleNo in ('+@billids+')'
exec(@sql)
select @time2=time2 from #tmp

-- 获取要上传的销售数据
set @sql='select b.SaleNo as billId,b.SaleDate,a.GoodsID,a.GoodsName,a.RetailPrice as price'
+',a.Qty,a.Amount,dbo.f_GetCookType(b.saleDate) as cooktype'
+',a.bigKindId,a.bigKindName,a.smallKindId,a.smallKindName'
+',a.UnitCode as unitid,a.Unit as unitname,'''' as vipid,'''' as vipname,b.Employee as skyid'
+',b.Createtor as skyname,'''' as ywyid,'''' as ywyname,'+''''
+convert(varchar,@time2,21)+''''+' as time2'
+' from pos_detail a'
+' inner join Pos_Master b on a.SaleNo=b.SaleNo'
+' where b.SaleNo in ('+@billIds+')'
exec (@sql)

drop table #tmp

set nocount off
END

原文地址:https://www.cnblogs.com/hnxxcxg/p/3858051.html