存储过程未在SQL中调用

时间:2015-01-16 05:45:29

标签: sql sql-server tsql stored-procedures ssms

我有两个存储过程,一个调用另一个。尽管编译中没有任何错误,但是在运行SearchProcedure时,outputprocedure不会从中调用。我看到没有理由不被召唤。 SearchProcedure到目前为止工作正常。 请指出可能存在的问题。

我知道OutputProcedure没有被调用,因为我尝试在其中使用Select语句。

以下是代码:

调用如下:

Exec SearchProcedure @firstname ='Simran', @middlename='kaur', @lastname = 'Khurana', @City='Delhi'

程序:

CREATE Procedure OutputProcedure
( 
 @LastNameFromUser nvarchar(20) = null, 
 @LastNameFromTable nvarchar(20), 
 @MiddleNameFromUser nvarchar(20) = null, 
 @MiddleNameFromTable nvarchar(20) = null, 
 @CityFromUser nvarchar(20) = null,
  @CityFromTable nvarchar(20) = null,
@Percentage int out
  )
AS
BEGIN
select 'OUTPUTPROCEDURECALLED'
declare @maxvalue int
DECLARE @variableTable TABLE (
    idx int identity(1,1),
    matchvalue nvarchar(15))

    INSERT INTO @variableTable(matchvalue) values ('MiddleName')
    INSERT INTO @variableTable(matchvalue) values ('LastName')
    INSERT INTO @variableTable(matchvalue) values ('City')

    SELECT * FROM @variableTable 
DECLARE @counter int
declare @sql nvarchar(100)
declare @sql2 nvarchar(25), @finalResult nvarchar(100)
declare @sql3 nvarchar(300), @tempresultStore nvarchar(20), @temp int, @temp2 int, @average int


SET @counter = 1
SELECT @maxvalue = (SELECT MAX(idx) FROM @variableTable)

select @maxvalue as 'MAXVALUE'
WHILE(@counter <= @maxvalue)
BEGIN
    DECLARE @colVar nvarchar(15)

    SELECT @colVar = matchvalue FROM @variableTable WHERE idx = @counter
    set @sql = 'declare' +' ' + '@Temp'+ @colVar 
    exec @sql
    select @sql as 'SQLFORDECLARATIONS'

    set @temp = CHARINDEX(' ',@sql)
    select @temp as 'resultofcharindex'
    set @temp2 = @temp + 1
    SELECT @temp2 AS 'AFTERADDING1'
    set @tempresultStore = Right(@sql, @temp2)
    SELECT @tempresultStore AS 'FINALCUTPART'
set @sql3 = 'SET ' +  @sql2 + '= set ' + ' ' + @tempresultStore + '=' + 'dbo.[Match' + @colVar + '](' + @colVar + 'FromUser' + ',' + @colVar + 'FromTable' + ',' + 0 + ')' 
EXEC @sql3   
select @sql3 as 'check sql query formed'

set @finalResult = @finalResult + @sql2
select @finalResult as 'SUM'
    SET @counter = @counter + 1
    select @counter as 'COUNTERVALUE'
    END
    set @Percentage = @finalResult/@maxvalue
    SELECT @Percentage AS 'FINALRESULT'
RETURN 
END





create procedure SearchProcedure
(
@firstname nvarchar(20),
@middlename nvarchar(20) = null,
@lastname nvarchar(20),
@DOB Date = null,
@SSN nvarchar(30)= null, 
@ZIP nvarchar(10)= null,
@StateOfResidence nvarchar(2)= null,
@City nvarchar(20)= null,
@StreetName nvarchar(20)= null,
@StreetType nvarchar(20)= null,
@BuildingNumber int= null,
@Aptnumber nvarchar(10)= null
)
As 
DECLARE @sSQL NVARCHAR(2000), @Where NVARCHAR(1000) = ' '
declare @Percent int,
@FN nvarchar(20),
@MN nvarchar(20) = null,
@LN nvarchar(20),
@DateOfB Date = null,
@SSNumber nvarchar(30)= null, 
@ZIPCode nvarchar(10)= null,
@StateOfRes nvarchar(2)= null,
@CityOfRes nvarchar(20)= null,
@StreetNameRes nvarchar(20)= null,
@StreetTypeRes nvarchar(20)= null,
@BuildingNumberRes int= null,
@AptnumberRes nvarchar(10)= null
set @Percent = 0
create table #results
(
firstname nvarchar(20) not null,
middlename nvarchar(20),
lastname nvarchar(20)not null,
PercentageMatch int not null,
DOB Date,
SSN nvarchar(30), 
ZIP nvarchar(10),
[State] nvarchar(2),
City nvarchar(20),
StreetName nvarchar(20),
StreetType nvarchar(20),
BuildingNumber int,
Aptnumber nvarchar(10)
 )

declare  c Cursor local static Read_only 
 for 
SELECT * from dbo.Patients where firstname = @FN


open c

fetch next from c into @FN,
@MN,
@LN,
@DateOfB,
@SSNumber, 
@ZIPCode,
@StateOfRes,
@CityOfRes,
@StreetNameRes,
@StreetTypeRes,
@BuildingNumberRes,
@AptnumberRes

while @@FETCH_STATUS = 0 BEGIN

/*set @Percent = dbo.[MatchLastName](@lastname, @LN, @Percent)
set @Percent = dbo.[MatchMiddleName](@middlename, @MN, @Percent)
set @Percent = dbo.[MatchCity](@City, @CityOfRes, @Percent)*/
Exec [dbo].[OutputProcedure]  @lastname,  @LN, @middlename, @MN,@City, @CityOfRes, @Percent output
Insert into #results values 
(@FN,@MN,@LN,@Percent, @DateOfB,@SSNumber, @ZIPCode,@StateOfRes,@CityOfRes,@StreetNameRes,@StreetTypeRes,@BuildingNumberRes,@AptnumberRes)
fetch next from c into @FN,
@MN,
@LN,
@DateOfB,
@SSNumber, 
@ZIPCode,
@StateOfRes,
@CityOfRes,
@StreetNameRes,
@StreetTypeRes,
@BuildingNumberRes,
@AptnumberRes
end

select * from #results order by PercentageMatch desc
IF OBJECT_ID('tempdb..#results') IS NOT NULL DROP TABLE #results
go

1 个答案:

答案 0 :(得分:1)

在SearchProcedure中,缺少设置变量fn的代码;用

打开光标
declare  c Cursor local static Read_only 
 for 
SELECT * from dbo.Patients where firstname = @FN

只有我不知道@fn的设置位置;如果光标没有行,则第二个过程不会被调用,因为循环不会运行。