jonnydepp
دوشنبه 18 آبان 1388, 20:26 عصر
دو جدول به نام های Table1 وTable2 دارم .
Table1(L:int,State:int) و Table2(id Autonumber,L int,R Varchar(50))
L در Table1 کلید اصلی است و درTable2 کلید خارجی است. برای درج رکورد در Table2 ابتدا باید چند شرط را بررسی کنیم:
1 – فیلد L باید در Table1 موجود باشد.2- فیلد State باید دارای مقدار خاصی باشد.(مثلا مقدار 1 یا 2 داشته باشد.)
همچنین بعد از عمل درج باید فیلد State در Table1 بهنگام شود .(مثلا دارای مقدار 3 شود.)
برای این کار در SQL یک Stored Procedure به صورت زیر ایجاد کردم.
Create PROCEDURE save
@L int,
@R varchar(50
AS
BEGIN
SET NOCOUNT ON;
IF Exists(select * From Table1 Where L = @L)
begin
Declare @State int
Set @State= (select Table1.State From Table1 Where L = @L)
IF (@State=1 OR @State=2)
begin
IF ((@State=1 And @R='ma') or (@State=2 And @R='es'))
begin
begin Tran NewSale
Insert Into Table2(L,R)
Values(@L,@R)
Update Table1 Set State=3 Where L =@L
Print 'Table1 updated'
commit tran NewSale
select * From T_Amval Where Label_No=@L
End
Else
Print ''
End
Else
Print ''
End
Else
Print ''
END
ودر دلفی برای متصل شدن به این StoredProcedure از کامپوننت AdoCommand به شکل زیراستفاده کردم .
With AdoCommand1 do
begin
Connection:= AdoConnection1;
CommandType:= cmdStoredProc;
CommandText:='Save';
Parameters.CreateParameter('@R',ftString,pdInput,5 0,edt1.Text);
Parameters.CreateParameter('@L',ftInteger,pdInput, 8,edt2.Text);
Prepared:=True;
Execute;
end;
در دلفی هنگام اجرا با خطای Application uses a value of the wrong type for the current operation متوقف می شود.
Table1(L:int,State:int) و Table2(id Autonumber,L int,R Varchar(50))
L در Table1 کلید اصلی است و درTable2 کلید خارجی است. برای درج رکورد در Table2 ابتدا باید چند شرط را بررسی کنیم:
1 – فیلد L باید در Table1 موجود باشد.2- فیلد State باید دارای مقدار خاصی باشد.(مثلا مقدار 1 یا 2 داشته باشد.)
همچنین بعد از عمل درج باید فیلد State در Table1 بهنگام شود .(مثلا دارای مقدار 3 شود.)
برای این کار در SQL یک Stored Procedure به صورت زیر ایجاد کردم.
Create PROCEDURE save
@L int,
@R varchar(50
AS
BEGIN
SET NOCOUNT ON;
IF Exists(select * From Table1 Where L = @L)
begin
Declare @State int
Set @State= (select Table1.State From Table1 Where L = @L)
IF (@State=1 OR @State=2)
begin
IF ((@State=1 And @R='ma') or (@State=2 And @R='es'))
begin
begin Tran NewSale
Insert Into Table2(L,R)
Values(@L,@R)
Update Table1 Set State=3 Where L =@L
Print 'Table1 updated'
commit tran NewSale
select * From T_Amval Where Label_No=@L
End
Else
Print ''
End
Else
Print ''
End
Else
Print ''
END
ودر دلفی برای متصل شدن به این StoredProcedure از کامپوننت AdoCommand به شکل زیراستفاده کردم .
With AdoCommand1 do
begin
Connection:= AdoConnection1;
CommandType:= cmdStoredProc;
CommandText:='Save';
Parameters.CreateParameter('@R',ftString,pdInput,5 0,edt1.Text);
Parameters.CreateParameter('@L',ftInteger,pdInput, 8,edt2.Text);
Prepared:=True;
Execute;
end;
در دلفی هنگام اجرا با خطای Application uses a value of the wrong type for the current operation متوقف می شود.