我對delphi/sql server有個小問題。我有一個SP來自存儲過程的返回值在delphi中總是爲空
GO
/****** Object: StoredProcedure [dbo].[sp_adaugaUser] Script Date: 6/30/2014 12:33:52 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_adaugaUser]
-- Add the parameters for the stored procedure here
@User varchar(50),
@Password varchar(32)--,
-- @errMesaj varchar(50)
AS
BEGIN
declare @sUser varchar(50),
@sPassword varchar(32),
@errMesaj varchar(100),
@exists bit,
@exists2 bit;
set @sUser = LTRIM(RTRIM(@User))
set @sPassword = LTRIM(RTRIM(@Password))
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
select @exists = COUNT(*) from sys.server_principals where name = @sUser
select @exists2 = COUNT(*) from sys.database_principals where name = @sUser
-- Insert statements for procedure here
if @exists = 1 begin
if @exists2 = 1 set @errMesaj = 1 --'Userul deja exista'
else begin
exec ('CREATE USER [' + @sUser + '] FOR LOGIN [' + @sUser + ']')
set @errMesaj = 2 --'S-a creat userul pentru login deja existent'
end
end
else begin
exec(' CREATE LOGIN [' + @sUser + '] WITH PASSWORD = ''' [email protected] + '''
CREATE USER [' + @sUser + '] FOR LOGIN [' + @sUser+']')
set @errMesaj = 3 --'S-a creat user si login'
end
RETURN @errMesaj
END
此SP將登錄名和用戶添加到服務器/數據庫中...並根據它做了什麼返回消息。
在Delphi我已經得到了有3個參數的TADOStoredProcedure:
1:@user - ftString,pdInput 2:@Password - ftString,pdInput 1:@errMesaj - ftString,pdReturnValue
我叫TADOStoredProcedure這樣的:
procedure TdataModule1.AdaugaUser(user, password : string); // procedura ce apeleaza o SP din BD pentru adaugarea de useri
begin
sp_adaugaUser.Parameters.ParamByName('@User').Value := user;
sp_adaugaUser.Parameters.ParamByName('@Password').Value := password;
sp_adaugaUser.Prepared := true ;
try
sp_adaugaUser.ExecProc;
finally
sp_adaugaUser.Prepared := false ;
sp_vizualizare_useri.Close;
sp_vizualizare_useri.Open;
test := sp_adaugaUser.Parameters.ParamByName('@errMesaj').Value;
end;
end;
,但由於某種原因,當我調試測試的值總是null
,爲什麼呢?
錯誤:
你有例外嗎?似乎執行用戶沒有權限創建登錄名或用戶 – deterministicFail
我已經獲得了許可,我是100%,因爲我正在用sa用戶連接到SQL。我更新了錯誤圖片的帖子。 – CiucaS
如果SQL Server允許您像上面那樣更改存儲過程,我確實感到很驚訝。我會期待一個錯誤消息,因爲RETURN應該返回一個整數值 - 不是varchar。 –