need help i have 2 tables below
tblA tblB
emp_no emp_no
cust_no emp_name
others....... cust_no
cust_name
in delphi i have dbgrid i create 2 lookup with
(1) ResultField = emp_name from tblB
keyfields = emp_no
lookupKeyfields = emp_no and
(2) lookupResultField cust_name from tblB
keyfields = emp_no
lookupKeyfields = emp_no
no problem on this but when i save using applyUpdate only the emp_no is saved to tblA i want to get also the cust_no of lookup and save also to tblA
what is the possible solution on this
thanks
Related
I have the following two tables:
Transaction_table:
Date
SSN
Cust_Num
Cust_Ind
1/1/21
111-1111
null
null
1/2/21
222-2222
null
null
1/3/21
null
null
null
Customer_table:
Date
SSN
Cust_Num
Cust_Name
1/1/21
111-1111
101
Adam
1/2/21
222-2222
102
Bobby
1/3/21
333-3333
103
Carter
How do I update the Cust_Num on the Transaction_table with the Cust_Num on the Customer_table when the SSN on both table equal each other? Additionally, how do I update the Cust_Ind to ‘SSN Match’ when SSN on both table equal each other? Thank you!
I tried the following code and received errors:
Update t
from Transaction_table as t,
Customer_table as src
set t.Cust_Num = src.Cust_Num,
t.Cust_ind = 'SSN_Match'
where t.SSN = src.SSN
I have 2 tables:
tbl1(idM int identity(1,1), Name nvarchar(50));
tbl2(idRow int identity(1,1), idM int, Data nvarchar(50));
They are queried via fdQuery1 and fdQuery2 (master:fdQuery1, masterfield:idM).
CachedUpdates = True on both fdQuery.
FDSchemaAdapter is connected to both also.
Also FDUpdateSQL1 and FDUpdateSQL2 connected to fdQuery1 and fdQuery2.
On inserting (fdQuery1.Insert) new row in fdQuery1 it's idM is shown as -2 by default, f.e.
FDUpdateSQL1 insert text:
INSERT INTO tbl1 (Name)
VALUES (:NEW_Name);
SELECT SCOPE_IDENTITY() AS idM
FDUpdateSQL2 insert text:
INSERT INTO tbl2 (idM, Data)
VALUES (:NEW_idM, :NEW_Data);
SELECT SCOPE_IDENTITY() AS idRow
On saving I execute this code:
FDSchemaAdapter.ApplyUpdates;
fdQuery1.CommitUpdates;
fdQuery2.CommitUpdates;
Target: fill form with DB-aware components connected to tbl1 and tbl2 and set tbl2.idM automatically.
How can I do this?
I tried OnUpdateRecord method but it still doesn't return idM from FDUpdateSQL1 insert.
fdQuery1.OnAfterPost field fdQuery1idM.asInteger also returns -2 value.
Current example result in tables:
tbl1
|idM|Name|
|--------|
|5 |Test|
tbl2
|idRow|idM|Data |
|------------------|
|10 |-2 |DataText|
Target tbl2
|idRow|idM|Data |
|------------------|
|10 |5 |DataText|
Thank you!
I am working on sql server I have two tables and I need to return records from the left table which are not found in the right table for that I am using left join like below query,
select #MID=MID,#MName=Name,#PID=PID,#PName=PName,#DID=DID from #CompanyDataInfo where id=#MCount
insert into #temp SELECT Top(1) f.Name,f.PID,f.PName,v.* FROM #CompanyDataInfo f
left join Employee v on v.Id=f.ID and v.DID=f.DID
where v.Id =#MID and v.DId = #DId and v.PId = #PId and v.CId =#CId and DATE_TIME between DATEADD(minute,-555,GETDATE()) and GETDATE() order by DATE_TIME desc
Result should be all rows from #CompanyDataInfo table while no record found in Employee table for related ID, I googled and use "v.Id is null" but not getting expected result
Is there any solution greatly appriciable
Thanks In advance
Your query is not using left join in correct way. You are using your right table reference in where clause. I try to correct it below but I don't have full information about your table schema. Please try this-
select
#MID = MID,
#MName = Name,
#PID = PID,
#PName = PName,
#DID = DID
from #CompanyDataInfo
where id = #MCount
insert into #temp
select
f.Name,
f.PID,
f.PName,
v.*
from #CompanyDataInfo f
left join Employee v on v.Id=f.ID and v.DID=f.DID
where f.Id = #MID and
f.DId = #DId and
f.PId = #PId and
f.CId = #CId and
f.DATE_TIME between DATEADD(minute,-555,GETDATE()) and GETDATE() and
v.Id is null
order by f.DATE_TIME desc
Add ...and v.Id is null to your where clause.
Firstname emp_Fullname
--------------------------------------
chetan Patel, Chetan
mike Shah, Mike
ronie Desai, Ronie
create proc stored_procedure
#firstnamer varchar(max)
#fullname varchar(max)
as
begin
select ......
from....
where Firstname in (SELECT Value FROM dbo.FnSplit(#firstname,','))
--and emp_Fullname in (SELECT Value FROM dbo.FnSplit(#fullname,','))
I want result for below statement
exec stored_procedure 'chetan,ronie', 'Patel, Chetan,Shah, Mike'
How can I pass more than 2 emp_fullname in parameter in given stored procedure? Below is my function dbo.FnSplit that worked for multi value Firstname parameter but not working multi value fullname parameter.
ALTER FUNCTION [dbo].[FnSplit]
(
#List nvarchar(2000),
#SplitOn nvarchar(5)
)
RETURNS #RtnValue table (Id int identity(1,1), Value nvarchar(100))
AS
BEGIN
WHILE(Charindex(#SplitOn, #List) > 0)
BEGIN
INSERT INTO #RtnValue (value)
SELECT
VALUE = ltrim(rtrim(Substring(#List, 1, Charindex(#SplitOn, #List) - 1)))
SET #List = SUBSTRING(#List, Charindex(#SplitOn, #List) + len(#SplitOn), len(#List))
END
INSERT INTO #RtnValue (Value)
SELECT
VALUE = ltrim(rtrim(#List))
RETURN
END
Firstname in (SELECT Value FROM dbo.FnSplit(#firstname,'|'))
and emp_Fullname in (SELECT Value FROM dbo.FnSplit(#fullname,'|'))
and I figured that still in SSRS double click on dataset click parmater instead of default value choose expression and set it to "join(#firstname.value,"|")" and samething for other "join(#fullname.value,"|")" and now run it. Multi valye parameter should work find by doing above procedure.
Thanks to my self lol:) it took me 3 days to figured, thought you guys can use it!
I have two tables.
1- student table & 2- Score table
I want to insert value at student table & insert multi value at Score table with SP to SQL Server 2008.
for EX:
ALTER proc [dbo].[InsertIntoScore]
(
#DateReg datetime,
#stdLastName nvarchar(50),
#stdFirstName nvarchar(50),
#Description nvarchar(500),
multi value as score table...
)
AS
DECLARE #Id AS INT
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Student(DateReg,stdLastName,stdFirstName,[Description])
VALUES (#DateReg,#stdLastName,#stdFirstName,#Description)
set #Id = SCOPE_IDENTITY()
insert multi value at Score table...
COMMIT
END TRY
BEGIN CATCH
IF ##TRANCOUNT > 0
ROLLBACK
END CATCH
please help me...
You should use Table-Valued Parameters
Create a Sql Type for the table you will pass in
CREATE TYPE dbo.ScoreType AS TABLE ( ScoreID int, StudentID int, etc.... )
pass your datatable from C# code into the stored procedure using the above defined type
ALTER proc [dbo].[InsertIntoScore]
( #DateReg datetime, #stdLastName nvarchar(50), #stdFirstName nvarchar(50),
#Description nvarchar(500), #tvpScore ScoreType)
AS
.....
INSERT INTO dbo.Score (ScoreID, StudentID, ....)
SELECT dt.ScoreID, #id, .... FROM #tvpScore AS dt;
in c# pass the datatable in this way
SqlCommand insertCommand = new SqlCommand("InsertIntoScore", sqlConnection);
SqlParameter p1 = insertCommand.Parameters.AddWithValue("#tvpScore", dtScore);
p1.SqlDbType = SqlDbType.Structured;
p1.TypeName = "dbo.ScoreType";
.....
insertCommand.ExecuteNonQuery();