問題描述
我正在嘗試將 XML 列中的一些 XML 數據插入到 SQL Server 2012 中的臨時表中.
I'm trying to insert some XML data from a XML column into a temp table in SQL Server 2012.
這是我當前的查詢
DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)
IF OBJECT_ID('tempdb..dbo.#txn','u') IS NOT NULL
BEGIN
PRINT '#temp exists! drop table'
DROP TABLE tempdb.dbo.#txn;
END
ELSE
BEGIN
PRINT '#temp does not exist! create table'
CREATE TABLE #txn
(
accountcode varchar(100),
tienda varchar(100),
caja varchar(100),
cajero varchar(100),
fecha varchar(100),
transaccion varchar(100),
itemcode varchar(100),
description varchar(100),
quantity numeric(10,3),
weight numeric(10,3),
qty_weight numeric(10,3),
unitprice numeric(15,3),
totalprice numeric(15,3),
vatcode varchar(100),
hashcode varchar(100),
anulado varchar(100)
)
END
SELECT @XML = [LoadedXML] FROM [dbo].[XmlImport]
EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML
INSERT INTO #txn (accountcode, tienda, caja, cajero, fecha, transaccion, itemcode, description, quantity, weight, qty_weight, unitprice, totalprice, vatcode, hashcode, anulado)
SELECT
CASE
WHEN codigotienda = 1 THEN '01'
END as accountcode,
tienda,
caja,
cajero,
fecha,
transaccion,
itemcode,
description,
quantity,
weight,
CASE
WHEN quantity IS NULL THEN weight
WHEN weight IS NULL THEN quantity
END as qty_weight,
unitprice,
totalprice,
CASE
WHEN vatcode = 4 THEN 'V0'
WHEN vatcode = 1 THEN 'V1'
WHEN vatcode = 2 THEN 'V2'
WHEN vatcode = 3 THEN 'V3'
WHEN vatcode is NULL THEN 'V0'
END AS vatcode,
hashcode,
anulado
FROM
OPENXML(@hDoc, 'tcpos-export/transactions/transaction/trans-item')
WITH
(
codigotienda [varchar](100) '../shop/code',
tienda [varchar](100) '../shop/description',
caja [varchar](100) '../till/code',
cajero [varchar](100) '../cashier/code',
fecha [varchar](100) '../beginning-timestamp',
transaccion [varchar](100) '../trans-num',
itemcode [varchar](100) 'code',
description [varchar](100) 'description',
quantity numeric(10,3) 'quantity',
weight numeric(10,3) 'weight',
unitprice numeric(15,3) 'unit-price',
totalprice numeric(15,3) 'taxable-amount',
vatcode [varchar](100) 'vat-code',
hashcode [varchar](100) 'hash-code',
anulado [varchar](100) 'delete-operator-id'
)
SELECT *
FROM #txn
WHERE hashcode IS NOT NULL
AND totalprice NOT LIKE '%-%'
AND unitprice NOT LIKE '%-%'
AND anulado IS NULL
ORDER BY
CAST(hashcode AS int)
--LEFT JOIN [MAXIMERCADODEMO].[dbo].OITM sap
--ON #txn.itemcode = sap.itemcode COLLATE SQL_Latin1_General_CP1_CI_AS
--where #txn.itemcode is null
--SELECT #txn.itemcode FROM #txn
--LEFT JOIN [MAXIMERCADODEMO].[dbo].OITM sap
--ON #txn.itemcode = sap.itemcode COLLATE SQL_Latin1_General_CP1_CI_AS
--where #txn.itemcode is null
EXEC sp_xml_removedocument @hDoc
這是第一次有效.當我第二次運行它時,它應該刪除臨時表,但我收到了這個錯誤:
This works the first time. When I run it a second time, it should drop the temp table, but I get this error instead:
#temp 不存在!創建表
#temp does not exist! create table
Msg 2714, Level 16, State 6, Line 11
數據庫中已經有一個名為#txn"的對象.
Msg 2714, Level 16, State 6, Line 11
There is already an object named '#txn' in the database.
我不知道你們是否建議我使用臨時表或在我的數據庫中創建一個真實的表來管理這種情況?
I don't know if you guys recommend me using a temp table or create a real table in my database to manage this situation?
推薦答案
這個
IF OBJECT_ID('tempdb..#txn','u') IS NOT NULL
應該
IF OBJECT_ID('tempdb..#txn', 'u') IS NOT NULL DROP TABLE #txn;
你甚至可以逃脫:
IF OBJECT_ID('tempdb..#txn') IS NOT NULL
一旦您進行此更改,您就不再需要為此檢查大的 IF 語句.
Once you make this change you no longer need the big IF statement checking for this.
這篇關于在 SQL Server 2012 中插入臨時表的問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!