有人可以帮我解决这个错误吗? Msg 512,Level 16,State 1,Procedure gen048upload,101行 子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。
这是我的代码:
ALTER PROCEDURE [dbo].[gen048upload]
-- Add the parameters for the stored procedure here
@fileDate varchar(20)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @IerrorCode int
DECLARE @error int,
@tDay varchar(2),
@tMonth varchar(2),
@tYear varchar(4)
SET @tMonth= MONTH(GETDATE())
SET @tDay= DAY(GETDATE())
SET @tYear= Year(GETDATE())
DECLARE @tempTable TABLE (
tempRow varchar(1000)
)
-- DECLARE @command varchar(1000)
-- SET @command = 'copy Y:\FTP\RMT\' + @fileDate + '.txt'
-- INSERT into @tempTable EXEC master..xp_cmdshell @command
DECLARE @fne_count int
SET @fne_count = (SELECT COUNT(*) FROM @tempTable WHERE tempRow = 'The system cannot find the file specified.')
IF @fne_count = 0
BEGIN
BEGIN TRAN
DECLARE @fileName varchar(100)
SET @fileName = @fileDate + '.txt'
CREATE TABLE #temp1
(
WHOLEROW varchar(8000)
)
DECLARE @CMD varchar(8000)
SET @CMD = 'BULK INSERT #temp1
FROM ''Y:\FTP\RMT\' +@fileName+ '''
WITH (DATAFILETYPE = ''char'', KEEPNULLS, FIRSTROW=1, MAXERRORS=0, ROWTERMINATOR = '''+char(0x0A)+''')'
EXEC(@CMD)
SET @error=0
SET @error = @error + @@ERROR
DECLARE @Flag bigint,@msg varchar(200)
IF EXISTS(SELECT ISNULL(MAX(flag),0) FROM gen_048)
BEGIN
SET @Flag =(SELECT ISNULL(MAX(flag),0) FROM gen_048)+1
END
ELSE
BEGIN
SET @Flag = 1
END
-- DELETE FROM #temp1 WHERE WHOLEROW IS NULL
-- DELETE FROM #temp1 WHERE WHOLEROW like '%Hostdate|Book Type|CCY|Portfolio|GUAVA |Chart Code Name|Input|Native debit amount|Native credit amount|Base Amount|Counterparty|Acct|Deal|Product|Source|Native amount%'
-- DELETE FROM #temp1 WHERE WHOLEROW like '%||||CHART CODE||Date|||||No|Ref|||%'
-- DELETE FROM #temp1 WHERE WHOLEROW = ''
DECLARE @inputTable table(
input_date varchar(100)
)
IF EXISTS (
SELECT fdate
FROM gen048_fdate
WHERE fdate IN (
SELECT DISTINCT REPLACE(dbo.fn_Parsename(WHOLEROW, '|', 8), CHAR(9), '')
FROM #temp1
)
)
IF EXISTS(SELECT fdate FROM gen048_fdate WHERE fdate = @input_date)
BEGIN
SET @msg = 'Error: File Exists, Duplication is not allowed'
END
ELSE
BEGIN
INSERT INTO gen048_fdate
SELECT DISTINCT REPLACE(dbo.fn_Parsename(WHOLEROW,'|',8), CHAR(9), '') FROM #temp1
SET @IerrorCode = @@ERROR
IF (@IerrorCode <> 0) GOTO PROBLEM
INSERT INTO gen_048 (
accname,
add1,
add2,
zip,
add3,
add4,
add5,
accdate,
bzip,
uname,
accnum,
pin)
SELECT
dbo.fn_Parsename(WHOLEROW,'|',0),
dbo.fn_Parsename(WHOLEROW,'|',1),
dbo.fn_Parsename(WHOLEROW,'|',2),
dbo.fn_Parsename(WHOLEROW,'|',3),
dbo.fn_Parsename(WHOLEROW,'|',4),
dbo.fn_Parsename(WHOLEROW,'|',5),
dbo.fn_Parsename(WHOLEROW,'|',6),
dbo.fn_Parsename(WHOLEROW,'|',7),
dbo.fn_Parsename(WHOLEROW,'|',8),-- input date
dbo.fn_Parsename(WHOLEROW,'|',9),
dbo.fn_Parsename(WHOLEROW,'|',10),
dbo.fn_Parsename(WHOLEROW,'|',11)
FROM #temp1
SET @IerrorCode = @@ERROR
IF (@IerrorCode <> 0) GOTO PROBLEM
SET @msg = 'File has been successfully uploaded'
END
END
ELSE
BEGIN
SET @msg = 'File for.' + @fileDate + 'not found. No file is uploaded.'
END
COMMIT TRAN
SELECT @msg as msg, @fileDate as filedate
PROBLEM:
IF (@IerrorCode <> 0)
BEGIN
ROLLBACK TRAN
SELECT @IerrorCode as msg
END
END
END
-- [dbo].[gen048upload] '07-07-2015'
答案 0 :(得分:1)
IF EXISTS (
SELECT fdate
FROM gen048_fdate
WHERE fdate IN (
SELECT DISTINCT REPLACE(dbo.fn_Parsename(WHOLEROW, '|', 8), CHAR(9), '')
FROM #temp1
)
)
更新 -
ALTER PROCEDURE [dbo].[gen048upload]
(
@fileDate VARCHAR(20)
)
AS BEGIN
SET NOCOUNT ON;
IF OBJECT_ID('tempdb.dbo.#temp') IS NOT NULL
DROP TABLE #temp
CREATE TABLE #temp (WHOLEROW VARCHAR(8000))
BEGIN TRY
BEGIN TRANSACTION
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = 'BULK INSERT #temp1
FROM ''Y:\FTP\RMT\' + @fileDate + '.txt' + '''
WITH (DATAFILETYPE = ''char'', KEEPNULLS, FIRSTROW=1, MAXERRORS=0, ROWTERMINATOR = ''' + CHAR(0x0A) + ''')'
EXEC sys.sp_executesql @SQL
IF EXISTS (
SELECT fdate
FROM gen048_fdate
WHERE fdate IN (
SELECT DISTINCT REPLACE(dbo.fn_Parsename(WHOLEROW, '|', 8), CHAR(9), '')
FROM #temp1
)
)
BEGIN
SELECT 'Error: File Exists, Duplication is not allowed', @fileDate
END
ELSE BEGIN
INSERT INTO gen048_fdate (fdate)
SELECT DISTINCT REPLACE(dbo.fn_Parsename(WHOLEROW,'|',8), CHAR(9), '')
FROM #temp1
INSERT INTO gen_048 (accname, add1, add2, zip, add3, add4, add5, accdate, bzip, uname, accnum, pin)
SELECT
dbo.fn_Parsename(WHOLEROW,'|',0),
dbo.fn_Parsename(WHOLEROW,'|',1),
dbo.fn_Parsename(WHOLEROW,'|',2),
dbo.fn_Parsename(WHOLEROW,'|',3),
dbo.fn_Parsename(WHOLEROW,'|',4),
dbo.fn_Parsename(WHOLEROW,'|',5),
dbo.fn_Parsename(WHOLEROW,'|',6),
dbo.fn_Parsename(WHOLEROW,'|',7),
dbo.fn_Parsename(WHOLEROW,'|',8),
dbo.fn_Parsename(WHOLEROW,'|',9),
dbo.fn_Parsename(WHOLEROW,'|',10),
dbo.fn_Parsename(WHOLEROW,'|',11)
FROM #temp1
SELECT 'File has been successfully uploaded', @fileDate
END
COMMIT TRANSACTION
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
IF XACT_STATE() <> 0 BEGIN
ROLLBACK TRANSACTION
END
END CATCH
END
答案 1 :(得分:0)
使用查询在变量中赋值时。查询必须返回一个值。 请检查此行
SET @input_date = (SELECT DISTINCT REPLACE(dbo.fn_Parsename(WHOLEROW,'|',8), CHAR(9), '') FROM #temp1)
在这一行中应该使用聚合函数。 的 MAX() Min()