0
有人幫助我在此代碼我嘗試執行,但它給了我很多的錯誤消息102,級別15,狀態1,行1個附近有語法錯誤 - 「
(2 row(s) affected)
(11942 row(s) affected)
(0 row(s) affected)
(0 row(s) affected)
(0 row(s) affected)
(0 row(s) affected)
(0 row(s) affected)
(0 row(s) affected)
(0 row(s) affected)
(1 row(s) affected)
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '-'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '-'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '-'.
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'then'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '-'.
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'then'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '-'.
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'then'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '-'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '-'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '-'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '-'.
(1 row(s) affected)
代碼:
USE [MyDB]
GO
/****** Object: StoredProcedure [dbo].[pull_from_pos] Script Date: 11/03/2015 17:32:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Batch submitted through debugger: SQLQuery16.sql|7|0|C:\Users\ADMINI~1\AppData\Local\Temp\2\~vs4CCB.sql
ALTER procedure [dbo].[pull_from_pos]
@srv nvarchar(50)
as
declare @sql nvarchar(max)
--declare @srv nvarchar(50)
declare @db nvarchar(50)
--select @srv=srvname+'.' from posmaster
select @db='.'+dbname+'.dbo.' from posmaster
--delete from msg1
IF EXISTS (SELECT * FROM sys.tables WHERE name='bill_no_table') DROP TABLE bill_no_table
set @sql='select bill_no into bill_no_table from '+ @[email protected]+'bill_mast where shift_id in (select shiftid from '+ @[email protected] +'shift_master where status=''c'')'
exec sp_executesql @sql
--if NOT exists(select bill_no,count(*) from transactions group by bill_no having count(*)>1)
--begin
--set identity_insert transactions on
set @sql ='MERGE transactions AS a
USING (select bill_no,Qty,Barcode,Unit,Unit_Price,Net_Price,Hold_id,Tran_Type,Unit_Name,VOID,amt_disc,serial,Eserl,Cost,PRICE_OVR,N_Bar_Code,N_Item_Descr,N_Found_Flag,tax from '[email protected][email protected]+'transactions) B
ON a.bill_no=b.bill_no
when not matched and b.bill_no in (select bill_no from bill_no_table)
then insert(bill_no,Qty,Barcode,Unit,Unit_Price,Net_Price,Hold_id,Tran_Type,Unit_Name,VOID,amt_disc,serial,Eserl,Cost,PRICE_OVR,N_Bar_Code,N_Item_Descr,N_Found_Flag,tax)
values(b.bill_no,b.Qty,b.Barcode,b.Unit,b.Unit_Price,b.Net_Price,b.Hold_id,b.Tran_Type,b.Unit_Name,b.VOID,b.amt_disc,b.serial,b.Eserl,b.Cost,b.PRICE_OVR,b.N_Bar_Code,b.N_Item_Descr,b.N_Found_Flag,b.tax);'
exec sp_executesql @sql
--set identity_insert transactions off
--end
if NOT exists(select bill_no,count(*) from bill_mast group by bill_no having count(*)>1)
begin
set identity_insert bill_mast on
set @sql ='MERGE bill_mast AS a
USING (select Bill_no,Cust_Id,Amt,Disc_amt,Net_Amt,Shift_Id,Loial_Points,Bill_Time,Cancled,Loial_Card,Loial_dis,user_id,CustomerName,CPR_No,Mobile_No,Office_No,Residence_No from '[email protected][email protected]+'bill_mast) B
ON a.bill_no=b.bill_no
when not matched and b.bill_no in (select bill_no from bill_no_table)
then insert(Bill_no,Cust_Id,Amt,Disc_amt,Net_Amt,Shift_Id,Loial_Points,Bill_Time,Cancled,Loial_Card,Loial_dis,user_id,CustomerName,CPR_No,Mobile_No,Office_No,Residence_No)
values(b.Bill_no,b.Cust_Id,b.Amt,b.Disc_amt,b.Net_Amt,b.Shift_Id,b.Loial_Points,b.Bill_Time,b.Cancled,b.Loial_Card,b.Loial_dis,b.user_id,b.CustomerName,b.CPR_No,b.Mobile_No,b.Office_No,b.Residence_No);'
exec sp_executesql @sql
set identity_insert bill_mast off
end
--set identity_insert pos_bill_payment on
--if NOT exists(select bill_no,PAYMENTGIVEN,count(*) from pos_bill_payment group by bill_no,PAYMENTGIVEN having count(*)>1)
begin
set @sql ='MERGE pos_bill_payment AS a
USING (select BILL_NO,PAYTYPE,PAYFCAMT,PAYamt,PAYMENTGIVEN,PAYMODETYPE,PAYMODE,card_no,BALANCE,RATE from '[email protected][email protected]+'pos_bill_payment) B
ON a.bill_no=b.bill_no and a.PAYMENTGIVEN =b.PAYMENTGIVEN
when not matched and b.bill_no in (select bill_no from bill_no_table)
then insert(BILL_NO,PAYTYPE,PAYFCAMT,PAYamt,PAYMENTGIVEN,PAYMODETYPE,PAYMODE,card_no,BALANCE,RATE)
values(b.BILL_NO,b.PAYTYPE,b.PAYFCAMT,b.PAYamt,b.PAYMENTGIVEN,b.PAYMODETYPE,b.PAYMODE,b.card_no,b.BALANCE,b.RATE);'
exec sp_executesql @sql
end
--set identity_insert pos_bill_payment off
if NOT exists(select shiftid,count(*) from shift_master group by shiftid having count(*)>1)
begin
set identity_insert shift_master on
set @sql ='MERGE shift_master AS a
USING (select Shiftid,shift_date,user_id,pos_id,Tstart,tend,status,disc_amt,tot_Float,Tot_Coll,Tot_Sales,Tot_Sales_Void,Tot_Sales_Return,NET_SALES,LN_DIS_AMT,LOYAL_DIS_AMT,TOT_FLOAT_LOCAL,TOT_FLOAT_FOR,TOT_FLOAT_OTHR,TOT_COLL_LOCAL,TOT_COLL_FOR,TOT_COLL_OTHR,TOT_FOR_PAY,TOT_LOC_PAY,TOT_PAY,TOT_OTHR_PAY,SHORTACCESS,tot_cc,tot_bd,tot_qtr,tot_kd,tot_sa,tot_uae,tot_chq,tot_cus,tot_usa,TOT_CN,No_Cust,TOTAMX from '[email protected][email protected]+'shift_master) B
ON a.shiftid=b.shiftid
when not matched and b.status=''c'' then insert(Shiftid,shift_date,user_id,pos_id,Tstart,tend,status,disc_amt,tot_Float,Tot_Coll,Tot_Sales,Tot_Sales_Void,Tot_Sales_Return,NET_SALES,LN_DIS_AMT,LOYAL_DIS_AMT,TOT_FLOAT_LOCAL,TOT_FLOAT_FOR,TOT_FLOAT_OTHR,TOT_COLL_LOCAL,TOT_COLL_FOR,TOT_COLL_OTHR,TOT_FOR_PAY,TOT_LOC_PAY,TOT_PAY,TOT_OTHR_PAY,SHORTACCESS,tot_cc,tot_bd,tot_qtr,tot_kd,tot_sa,tot_uae,tot_chq,tot_cus,tot_usa,TOT_CN,No_Cust,TOTAMX)
values(b.Shiftid,b.shift_date,b.user_id,b.pos_id,b.Tstart,b.tend,b.status,b.disc_amt,b.tot_Float,b.Tot_Coll,b.Tot_Sales,b.Tot_Sales_Void,b.Tot_Sales_Return,b.NET_SALES,b.LN_DIS_AMT,b.LOYAL_DIS_AMT,b.TOT_FLOAT_LOCAL,b.TOT_FLOAT_FOR,b.TOT_FLOAT_OTHR,b.TOT_COLL_LOCAL,b.TOT_COLL_FOR,b.TOT_COLL_OTHR,b.TOT_FOR_PAY,b.TOT_LOC_PAY,b.TOT_PAY,b.TOT_OTHR_PAY,b.SHORTACCESS,b.tot_cc,b.tot_bd,b.tot_qtr,b.tot_kd,b.tot_sa,b.tot_uae,b.tot_chq,b.tot_cus,b.tot_usa,b.TOT_CN,b.No_Cust,b.TOTAMX);'
exec sp_executesql @sql
set identity_insert shift_master off
end
if NOT exists(select day_end_id,count(*) from day_end_master group by day_end_id having count(*)>1)
begin
set identity_insert day_end_master on
set @sql ='MERGE day_end_master AS a
USING (select day_end_id,Day_end_date,pos_id,Tstart,tend,status,disc_amt,tot_Float,Tot_Coll,Tot_Sales,Tot_Sales_Void,Tot_Sales_Return,NET_SALES,LN_DIS_AMT,LOYAL_DIS_AMT,TOT_FLOAT_LOCAL,TOT_FLOAT_FOR,TOT_FLOAT_OTHR,TOT_COLL_LOCAL,TOT_COLL_FOR,TOT_COLL_OTHR,TOT_LOC_PAY,TOT_FOR_PAY,TOT_OTHR_PAY,TOT_PAY,SHORTACCESS from '[email protected][email protected]+'day_end_master) B
ON a.day_end_id=b.day_end_id
when not matched and b.status=''c'' then insert(day_end_id,Day_end_date,pos_id,Tstart,tend,status,disc_amt,tot_Float,Tot_Coll,Tot_Sales,Tot_Sales_Void,Tot_Sales_Return,NET_SALES,LN_DIS_AMT,LOYAL_DIS_AMT,TOT_FLOAT_LOCAL,TOT_FLOAT_FOR,TOT_FLOAT_OTHR,TOT_COLL_LOCAL,TOT_COLL_FOR,TOT_COLL_OTHR,TOT_LOC_PAY,TOT_FOR_PAY,TOT_OTHR_PAY,TOT_PAY,SHORTACCESS)
values(b.day_end_id,b.Day_end_date,b.pos_id,b.Tstart,b.tend,b.status,b.disc_amt,b.tot_Float,b.Tot_Coll,b.Tot_Sales,b.Tot_Sales_Void,b.Tot_Sales_Return,b.NET_SALES,b.LN_DIS_AMT,b.LOYAL_DIS_AMT,b.TOT_FLOAT_LOCAL,b.TOT_FLOAT_FOR,b.TOT_FLOAT_OTHR,b.TOT_COLL_LOCAL,b.TOT_COLL_FOR,b.TOT_COLL_OTHR,b.TOT_LOC_PAY,b.TOT_FOR_PAY,b.TOT_OTHR_PAY,b.TOT_PAY,b.SHORTACCESS);'
exec sp_executesql @sql
set identity_insert day_end_master off
end
if NOT exists(select day_end_id,count(*) from day_end_master group by day_end_id having count(*)>1)
begin
set @sql ='MERGE shift_end_cash AS a
USING (select code,name,famt,lamt,shift_id from '[email protected][email protected]+'shift_end_cash) B
ON a.code=b.code and a.shift_id=b.shift_id
when not matched then insert(code,name,famt,lamt,shift_id)
values(b.code,b.name,b.famt,b.lamt,b.shift_id);'
exec sp_executesql @sql
end
if NOT exists(select User_id,count(*) from collection_master group by User_id having count(*)>1)
begin
set @sql ='MERGE collection_master AS a
USING (select User_id,User_Name,Pos_id,Shift_id,ddate,code,QTY,RATE,AMT,Colle_Type,usr,timestamp,Colle_Id,Total_amt,lblname,txtQNAME,TXTAMTNAME,status from '[email protected][email protected]+'collection_master) B
ON a.Colle_Id=b.Colle_Id and a.pos_id=b.pos_id
when not matched then insert(User_id,User_Name,Pos_id,Shift_id,ddate,code,QTY,RATE,AMT,Colle_Type,usr,timestamp,Colle_Id,Total_amt,lblname,txtQNAME,TXTAMTNAME,status)
values(User_id,User_Name,Pos_id,Shift_id,ddate,code,QTY,RATE,AMT,Colle_Type,usr,timestamp,Colle_Id,Total_amt,lblname,txtQNAME,TXTAMTNAME,status);'
exec sp_executesql @sql
end
if NOT exists(select Float_Id,count(*) from float_master group by Float_Id having count(*)>1)
begin
set identity_insert float_master on
set @sql ='MERGE float_master AS a
USING (select User_id,User_Name,Pos_id,Shift_id,ddate,code,QTY,RATE,AMT,Float_Type,trn,usr,timestamp,Float_Id,Total_amt,lblname,txtQNAME,TXTAMTNAME,status from '[email protected][email protected]+'float_master) B
ON a.User_id=b.User_id and a.pos_id=b.pos_id and a.shift_id=b.shift_id
when not matched then insert(User_id,User_Name,Pos_id,Shift_id,ddate,code,QTY,RATE,AMT,Float_Type,trn,usr,timestamp,Float_Id,Total_amt,lblname,txtQNAME,TXTAMTNAME,status)
values(User_id,User_Name,Pos_id,Shift_id,ddate,code,QTY,RATE,AMT,Float_Type,trn,usr,timestamp,Float_Id,Total_amt,lblname,txtQNAME,TXTAMTNAME,status);'
exec sp_executesql @sql
set identity_insert float_master off
end
if NOT exists(select day_date,pos_id ,count(*) from pos_day_dates group by day_date,pos_id having count(*)>1)
begin
--set identity_insert pos_day_dates on
set @sql ='MERGE pos_day_dates AS a
USING (select day_date,pos_id,status from '[email protected][email protected]+'pos_day_dates) B
ON a.day_date=b.day_date and a.pos_id=b.pos_id
when not matched and b.status=''c'' then insert(day_date,pos_id,status)
values(b.day_date,b.pos_id,b.status);'
exec sp_executesql @sql
--set identity_insert pos_day_dates off
end
--set @sql ='MERGE AS a
--USING (select from '[email protected][email protected]+') B
--ON a.code=b.code
--when not matched then insert()
--values();'
--exec sp_executesql @sql
服務器和數據庫的名稱是否包含' - '?當然如果包裹在方括號中的話? –
嘗試使用QUOTENAME(dbname)和QUOTENAME(@srv) –
Sproc和錯誤幫助,但可以請發送您正在運行的實際查詢嗎? – Tigerjz32