insert into SDORGDESCGL(COMP_CD ,ORG_CODE)
values ('1',(select (select case when '60'='60' then '0001' when '60' = '61' then '0002' end)+'00000000000000000000000000000000'))
------新编辑-----
*我已经简化了主要问题。第二个值数据输入错误。
我的问题是: 为什么我会收到以下错误?当我单独编译每个语句(每个Values语句)时,它工作正常。
错误:
Msg 1046,Level 15,State 1,Line 2 在此上下文中不允许子查询。只允许使用标量表达式。Msg 102,Level 15,State 1,Line 2 ' +'。
附近的语法不正确
答案 0 :(得分:2)
编辑:您的查询适用于SQL Server 2008 R2。您的配置不同,或者您有不同版本的SQL Server。
如果您使用的是旧版本,可以尝试以下操作:
insert into #test(COMP_CD ,ORG_CODE)
Select '1',(select (select case when '60'='60' then '0001' when '60' = '61' then '0002' end)+ ('00000000000000000000000000000000'))
原始答案:
IF OBJECT_ID('tempdb.dbo.#test') IS NOT NULL
BEGIN
DROP TABLE #test
END
CREATE TABLE #test
(comp_cd VARCHAR(1000),
org_code VARCHAR(1000),
node_desc VARCHAR(1000),
parentorg_cd VARCHAR(1000),
user_id VARCHAR(1000),
rec_mode VARCHAR(1000))
INSERT INTO #test
(comp_cd,
org_code,
node_desc,
parentorg_cd,
user_id,
rec_mode)
OUTPUT INSERTED.*
VALUES ('1',
(SELECT (SELECT LV1 = CASE
WHEN '60' = '60' THEN '0005'
WHEN '60' = '61' THEN '0002'
END)
+ (SELECT RIGHT('0000' + CONVERT(VARCHAR, (SELECT(SELECT
TOP 1 Substring
(
org_code,
5, 4)
AS SUB
FROM #test WHERE comp_cd =
1 ORDER BY
sub DESC)+
1)
), 4))
+ '0000000000000000000000000000'),
(SELECT '822000' + ' - '
+ 'Receiving & W/house - Selangor'),
(SELECT (SELECT CASE
WHEN '60' = '60' THEN '0001'
WHEN '60' = '61' THEN '0002'
END)
+ '00000000000000000000000000000000'),
'SMHIGWN',
'A')
答案 1 :(得分:1)
我尝试重新格式化并重写代码,至少在语法上是正确的:
with cte as
(
SELECT top 1 SUBSTRING(org_code,5,4) AS SUB
FROM SDORGDESCGL
WHERE COMP_CD = 1
ORDER BY SUB DESC
)
INSERT INTO SDORGDESCGL(COMP_CD ,ORG_CODE,NODE_DESC,PARENTORG_CD,USER_ID,REC_MODE)
SELECT
'1',
CASE WHEN '60' = '60' THEN '0005' WHEN '60' = '61' THEN '0002' END
+ RIGHT('0000'+ CONVERT(VARCHAR,SUB+1)),4) + '0000000000000000000000000000',
'822000'+' - '+'Receiving & W/house - Selangor',
CASE WHEN '60'='60' THEN '0001' WHEN '60' = '61' THEN '0002' END +'00000000000000000000000000000000',
'SMHIGWN',
'A'
FROM cte
然而,逻辑似乎并不正确,因为两个CASE语句将始终返回true(由于' 60' =' 60')...