EXEC dbo.Alpha @BatchName = 'Test',@BatchNumber = '2',@Profile = 'SQLTEST'
这给了我一个错误
','
附近的语法不正确
我尝试删除或添加每个逗号。 SQL Server 2008 Express和Management Studio。
以下是参考程序:
CREATE PROCEDURE Alpha
@BatchName NVARCHAR(50),
@BatchNumber NVARCHAR(50),
@Profile NVARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Batch_Reports NVARCHAR(50)
SET @Batch_Reports = @BatchName + '_' + @BatchNumber + '_reports'
DECLARE @Memory TABLE (docNumber NVARCHAR(50),SortOrder NVARCHAR(50));
DECLARE @Thought NVARCHAR(1000)
SET @Thought = 'INSERT INTO Memory (docNumber,SortOrder)
SELECT (docNumber,SortOrder) FROM dbo.' + @Batch_Reports
EXEC (@Thought)
DECLARE @GoldFish NVARCHAR(50)
DECLARE lost CURSOR FOR
SELECT DISTINCT docNumber
FROM @Memory
OPEN lost
FETCH NEXT FROM lost INTO @GoldFish
WHILE @@FETCH_STATUS = 0
BEGIN
IF NOT EXISTS (SELECT BatchIndexing.DocumentIndexID
FROM BatchIndexing
WHERE BatchIndexing.docNumber = @GoldFish
AND BatchIndexing.BatchName = @BatchName + ' ' + @BatchNumber)
BEGIN
INSERT INTO BatchIndexing (BatchName, docNumber, ProfileUsed, TotalPages)
VALUES (@BatchName + ' ' + @BatchNumber, @GoldFish, @Profile, '0');
SELECT SCOPE_IDENTITY() AS [DocumentIndexID]
DECLARE @Identity NVARCHAR(50)
SET @Identity = (SELECT DocumentIndexID
FROM BatchIndexing
WHERE BatchIndexing.docNumber = @GoldFish
AND BatchIndexing.BatchName = @BatchName + ' ' + @BatchNumber)
INSERT INTO IndexingFields (DocumentIndexID, FieldName, FieldValue)
VALUES(@Identity, 'Barcode', (NULL))
INSERT INTO IndexingFields (DocumentIndexID, FieldName, FieldValue)
VALUES (@Identity, 'Docket #', (NULL))
INSERT INTO IndexingFields (DocumentIndexID, FieldName, FieldValue)
VALUES (@Identity, 'Respondent Name', (NULL))
END
ELSE
BEGIN
FETCH NEXT FROM lost INTO @GoldFish
END
END
DECLARE @Omega TABLE (DocumentIndexID NVARCHAR(50),
BatchName NVARCHAR(50),
docNumber NVARCHAR(50),
ProfileUsed NVARCHAR(50),
TotalPages NVARCHAR(50),
SortOrder NVARCHAR(50)
);
SELECT
[BI].[DocumentIndexID],
[BI].[BatchName],
[BI].[docNumber],
[BI].[ProfileUsed],
[AR].[SortOrder]
FROM
BatchIndexing BI
INNER JOIN
@Memory AR ON [BI].[docNumber] = [AR].[docNumber]
WHERE
[BI].[BatchName] = @BatchName + ' ' + @BatchNumber
ORDER BY
[AR].[SortOrder] ASC
DECLARE @Heather NVARCHAR(50)
DECLARE found CURSOR FOR
SELECT DISTINCT SortOrder
FROM @Omega
OPEN found
FETCH NEXT FROM found INTO @Heather
DECLARE @I NVARCHAR(50)
SET @I = (SELECT DocumentIndexID FROM @Omega WHERE SortOrder = '1')
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @Spawn NVARCHAR(50)
SET @Spawn = (SELECT IndexingFields.FieldValue FROM IndexingFields
WHERE IndexingFields.FieldName = 'Docket #' AND IndexingFields.DocumentIndexID = (SELECT DocumentIndexID FROM @Omega
WHERE SortOrder = @Heather))
IF @Spawn IS NULL
BEGIN
UPDATE IndexingFields
SET FieldValue = (SELECT FieldValue FROM IndexingFields WHERE DocumentIndexID = @I AND FieldName = 'Docket #')
WHERE DocumentIndexID = (SELECT DocumentIndexID FROM @Omega WHERE SortOrder = @Heather)
AND FieldName = 'Docket #';
END
ELSE
BEGIN
SET @I = (SELECT DocumentIndexID FROM @Omega WHERE SortOrder = @Heather)
FETCH NEXT FROM found INTO @Heather
END
END
DECLARE @Dori NVARCHAR(50)
DECLARE favor CURSOR FOR SELECT DISTINcT SortOrder FROM @Omega
OPEN favor
FETCH NEXT FROM favor INTO @Dori
SET @I = (SELECT DocumentIndexID FROM @Omega WHERE SortOrder = '1')
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Spawn = (SELECT FieldValue FROM IndexingFields
WHERE IndexingFields.FieldName = 'Barcode' AND IndexingFields.DocumentIndexID = (SELECT DocumentIndexID FROM @Omega
WHERE SortOrder = @Heather))
IF @Spawn IS NULL
BEGIN
UPDATE IndexingFields
SET FieldValue = (SELECT FieldValue FROM IndexingFields WHERE DocumentIndexID = @I AND FieldName = 'Barcode')
WHERE DocumentIndexID = (SELECT DocumentIndexID FROM @Omega WHERE SortOrder = @Dori)
AND FieldName = 'Barcode'
DECLARE @Administrator NVARCHAR(50)
SET @Administrator = 'UPDATE ' + @Batch_Reports + ' SET IsIndexed = 1
WHERE docNumber = Omega.docNumber
AND Omega.SortOrder = ' + @Heather
EXEC (@Administrator)
DECLARE @Transporter NVARCHAR(50)
SET @Transporter = 'UPDATE Batches SET CurrentModule = QC Index
WHERE BatchName = ' + @BatchName + ' ' + @BatchNumber
EXEC (@Transporter)
END
ELSE
BEGIN
SET @I = (SELECT DocumentIndexID FROM @Omega WHERE SortOrder = @Dori)
FETCH NEXT FROM found INTO @Heather
END
END
END
GO
这是一个很小的工作项目,我一直在努力。我知道会有很多其他/更好的方法来做到这一点。虽然我很感激这一点,但我真的更感兴趣的是做我想做的事情。这基本上是将所有空字段设置为最后一个不为空的字段。