SQL stored procedure results solution - sql

I have a stored procedure that I inherited. The goal is for the data in the temp table: #multi_nominees_uf to be joined with the data in #temp_reviewers_UF and assign #temp_reviewers_UF.uf_rev_id to #multi_nominees_uf.application_number where the corresponding uf_rev_id's short_plan does not match the major associated with the appNumber's major .
It is not as simple as doing a JOIN.
The following has to be in place:
short_plan can not match the major(associated with the uf_rev_id)
count of each uf_rev_id can only be in the table a certain number of times (#RevsPerRevieweruf). Also, the uf_rev_id will be in the #temp_reviewers_uf table more than once with a different short_plan, it should only be looking at DISTINCT uf_rev_id when calculating the #RevPerRevieweruf.
The way it is written now, the counts are not consistent. One uf_rev_ID may have 122 records and another may have 50 - each distinct uf_rev_id should have the same count (or very close). I have researched and tried NTILE but I couldn't figure it out.
Any ideas of the best way to accomplish this?? Any input is appreciated.
-----Sample Data -----
CREATE TABLE #mult_nominees_uf(
appnum VARCHAR(8)
,major VARCHAR(8)
,compid INT
);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('00012345','ACT',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10002343','BBC',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10002777','BBC',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10000023','DED',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('23457829','AAR',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('78954321','RRE',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('90002342','ACT',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('11156726','AAR',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('88855593','RRE',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10000001','DED',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('20000393','ACT',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('11119999','DED',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('78927626','AAR',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('67589393','RRE',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('12453647','AAR',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('00012345','ACT',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10002343','BBC',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10002777','BBC',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10000023','DED`',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('23457829','AAR',2);
--with this sample data the #RevsPerReviewerUF count would be 4 since A5 is listed twice and we only want distinct values used
CREATE TABLE #Temp_Reviewers_uf(
uf_rev_id VARCHAR(8)
,short_plan VARCHAR(8)
,fac_emplid INTEGER
);
INSERT INTO #Temp_Reviewers_uf(uf_rev_id,short_plan,fac_emplid) VALUES ('A1','ACT',00000012);
INSERT INTO #Temp_Reviewers_uf(uf_rev_id,short_plan,fac_emplid) VALUES ('A2','BBC',00000145);
INSERT INTO #Temp_Reviewers_uf(uf_rev_id,short_plan,fac_emplid) VALUES ('A3','DED',10002934);
INSERT INTO #Temp_Reviewers_uf(uf_rev_id,short_plan,fac_emplid) VALUES ('A5','RRE',90001223);
INSERT INTO #Temp_Reviewers_uf(uf_rev_id,short_plan,fac_emplid) VALUES ('A5','ACT',90001223);
----Stored procedure -
DECLARE #Index INT
DECLARE #Num_nomineesUF INT
DECLARE #Num_reviewersUF INT
DECLARE #Num_reviewersUFDISTINCT INT
DECLARE #Num_reviews INT
DECLARE #Rev_ID nvarchar(25), #Nom_ID varchar(8), #Short_Plan varchar(8), #Major varchar(8)
DECLARE #RevsPerReviewerUF INT
SET #Num_reviews = 4
DECLARE #actualCompID int
DECLARE #UF_Flag INT
DECLARE #InsertNum int
SET #InsertNum = 1
create table #mult_nominees_UF (appNumber varchar(8), Major varchar(8), comp_id INT)
create table #TempNomineeTable (uf_rev_id varchar(8), fac_emplid varchar(9), appNumber varchar(8), Major varchar(8), short_plan varchar(8), comp_id int)
create table #Temp_Reviewers_UF (uf_rev_id varchar(8), short_plan varchar(8), fac_emplid varchar(9)) -- temp table used to hold Nom_IDs already assigned to Rev_IDs
set #actualCompID = 21
-- * * SELECT APPLICATION NUMBER & MAJOR FROM FS_RESULTS TABLE * * * * * --
select appNumber, LEFT(Major, CHARINDEX('-', Major)-1) as Major, comp_id into #Delete_nomineesUF
from FS_Results
where UF='Y'
and comp_id = #actualCompID
and nominated=1;
SET #Num_nomineesUF = ##rowcount; --GET RECORD COUNT
IF (#Num_nomineesUF > 0)
BEGIN
SET #UF_Flag = 1;
END
SET #Index = 1 ; -- reinit variable
WHILE #Index <= 4 BEGIN
if (#UF_Flag > 0)
BEGIN
INSERT into #mult_nominees_uf
select * from #Delete_nomineesUF
END
-- Create temp table for UF Reviewers
select uf_rev_id, short_plan, fac_emplid into #temp_reviewers_UF
from ReviewersID_ShortPlan
where uf_rev_id like 'UF%'
and competition_id = #actualCompID
SET #Num_reviewersUF = ##rowcount
SELECT DISTINCT UF_REV_ID FROM ReviewersID_ShortPlan WHERE UF_REV_ID like 'UF%' AND competition_id = #actualCompID
SET #Num_reviewersUFDistinct = ##rowcount
SET #RevsPerReviewerUF = (#Num_nomineesUF * #Num_reviews) / nullif(#Num_reviewersUFDistinct,0)
WITH Match_NomineesWithReviewers AS(
SELECT DISTINCT
appNumber,
RTRIM(Major) AS Major,
COUNT(1) as rowcnt,
comp_id
FROM #mult_nominees_uf
GROUP BY appNumber,
RTRIM(Major),
comp_id
)
, rownum_matches AS (
SELECT m.appNumber,
m.Major,
t.short_plan,
t.uf_rev_id,
t.fac_emplid,
m.rowcnt,
m.comp_id,
ROW_NUMBER() OVER (PARTITION BY m.appNumber order by newid()) AS rownum
FROM Match_NomineesWithReviewers m
JOIN #temp_reviewers_UF t ON t.short_plan != m.major
GROUP BY m.appNumber, m.Major, t.short_plan,
t.uf_rev_id, t.fac_emplid, m.rowcnt, m.comp_id
HAVING COUNT(t.uf_rev_id) <= #RevsPerRevieweruf
)
INSERT INTO #TempNomineeTable
SELECT uf_rev_id, fac_emplid, appNumber, Major, short_plan, null, 0, null, comp_id FROM rownum_matches rm
WHERE rownum <= rowcnt
group by uf_rev_id, fac_emplid, appNumber, Major, short_plan, comp_id
HAVING COUNT(uf_rev_id) <= #RevsPerRevieweruf

Related

is there way to apply row updates without looping

i would like to pay for different invoices using different credits i have.
drop table #InvoicesWithBalances
drop table #AvailableCredits
create table #InvoicesWithBalances
(
InvoiceKey decimal(18,0) not null,
APBalance decimal(18,6) null,
BalanceAfterCreditApplied decimal(18,6) null,
)
create table #AvailableCredits
(
credit_id int identity(1,1),
StartingBalance decimal(18,6) null,
CurrentBalance decimal(18,6) null,
)
insert into #InvoicesWithBalances values (5452, 13744.080000, 13744.080000)
insert into #InvoicesWithBalances values (7056, 13744.080000, 13744.080000)
insert into #InvoicesWithBalances values (7438, 500.000000, 500.000000 )
insert into #AvailableCredits values ( -13744.080000, -13744.080000)
insert into #AvailableCredits values ( -13700.080000, -13700.080000)
insert into #AvailableCredits values ( -500.000000, -500.000000)
insert into #AvailableCredits values ( -500.000000, -500.000000)
select * from #InvoicesWithBalances
select * from #AvailableCredits
If I was doing a looping solution I would take the largest credit and start applying it to the invoices in order of largest to smallest until the balance of the credit is zero, then I would move on to the next to the next credit until I had no credits and no invoices left.
In the example below the first 2 credits should be fully used. The third credit should be partially used and the last credit should go untouched
Any advice?
I have tried to simulate your example here:
create table InvoicesWithBalances
(
InvoiceKey int not null,
APBalance int null,
BalanceAfterCreditApplied int null,
);
create table AvailableCredits
(
credit_id int identity(1,1),
StartingBalance int null,
CurrentBalance int null,
);
insert into InvoicesWithBalances values (5452, 13744, 13744);
insert into InvoicesWithBalances values (7056, 13744, 13744);
insert into InvoicesWithBalances values (7438, 500, 500);
insert into AvailableCredits values ( -13744, -13744);
insert into AvailableCredits values ( -13700, -13700);
insert into AvailableCredits values ( -500, -500);
insert into AvailableCredits values ( -500, -500);
create table #invoice (invoice_row_num int, InvoiceKey int, APBalance int, BalanceAfterCreditApplied int);
insert into #invoice
select ROW_NUMBER() OVER (ORDER BY APBalance desc) as row_num, InvoiceKey, APBalance, BalanceAfterCreditApplied FROM InvoicesWithBalances;
create table #credits (credit_row_num int, StartingBalance int, CurrentBalance int);
insert into #credits
select ROW_NUMBER() OVER (ORDER BY StartingBalance asc) as row_num, StartingBalance, CurrentBalance FROM AvailableCredits;
create table #invoice_credit_list (invoice_credit_row_num int, init_invoice int, init_credit int);
if ((select max(invoice_row_num) from #invoice) > (select max(credit_row_num) from #credits))
insert into #invoice_credit_list
select i.invoice_row_num , i.APBalance, (-isnull(c.StartingBalance,0)) from
#invoice i
left join
#credits c
on
i.invoice_row_num = c.credit_row_num;
else
insert into #invoice_credit_list
select c.credit_row_num, isnull(i.APBalance,0), (-c.StartingBalance) from
#credits c
left join
#invoice i
on
i.invoice_row_num = c.credit_row_num;
with cte as
(
select
invoice_credit_row_num,
init_invoice,
init_credit,
case when init_invoice >= init_credit then
init_invoice - init_credit
else
0
end as 'invoice_remaining',
case when init_credit >= init_invoice then
init_credit - init_invoice
else
0
end as 'credit_remaining'
from
#invoice_credit_list i
where
i.invoice_credit_row_num = 1
UNION ALL
select
i.invoice_credit_row_num,
i.init_invoice + cte.invoice_remaining as 'init_invoice',
i.init_credit + cte.credit_remaining as 'init credit',
case when (i.init_invoice + cte.invoice_remaining) >= (i.init_credit + cte.credit_remaining ) then
(i.init_invoice + cte.invoice_remaining) - (i.init_credit + cte.credit_remaining )
else
0
end as 'invoice_remaining',
case when (i.init_credit + cte.credit_remaining) >= (i.init_invoice + cte.invoice_remaining) then
(i.init_credit + cte.credit_remaining) - (i.init_invoice + cte.invoice_remaining)
else
0
end as 'credit_remaining'
from
#invoice_credit_list i
inner join
cte
ON
i.invoice_credit_row_num - 1 = cte.invoice_credit_row_num
AND
i.invoice_credit_row_num > 1
)
select * from cte;
and you can also find this simulation with output here: https://rextester.com/SJYGV76640
The 'cte' table in the simulation will give you all the details.
Eventhough, this is now done in db side, I am not sure of its performance. So, please compare and evaluate its performance.
Note:
if this is performing faster, well and good. But, at most, don't opt for loops in T-SQL.
If this is not performing better, go for loops in any other programming language like C#, VB,.. if that is possible.
If no other option works for you, go for loops in T-SQL. But, with increasing data, I am not sure how the server will react :(
Hope this helps you :)

In sql table insert two same values,if insert same value in 3rd time it not allow to insert that record

create table sample(id int primary key,name varchar(100))
insert into sample values(1,'a')
,(2,'a')
,(3,'d')
,(4,'b')
,(5,'b')
--insert into sample values(6,'a'),(7,'b')
this record is not allow to insert the table.it disply error
Easiest solution is to check the table if the value being inserted is already present in the table twice before the insert statement.
--Preparation
DECLARE #sample TABLE
(
id INT IDENTITY PRIMARY KEY --USE IDENTITY to auto increment your primary key
,name VARCHAR(100)
)
--Initial Values
INSERT INTO #sample
VALUES
('a')
,('a')
,('d')
,('b')
,('b')
DECLARE #name VARCHAR(100)
SET #name = 'a'
IF(SELECT COUNT(id) FROM #sample WHERE name = #name) <= 1
BEGIN
INSERT INTO #sample
VALUES (#name)
END
ELSE
BEGIN
SELECT 'Error: Name ''' + #name + ''' already exists twice. Only two same values are allowed in name field!'
END

Banking Transaction in SQL

I have a table named BankTransaction:
Create table BankTransaction
(
TransactionID int IDENTITY(1,1),
AccountNumber varchar(25) Not Null,
TransactionDate datetime not null Default getdate(),
TransactionType varchar(25) Not Null,
TransactionAmount money Default '0',
BalanceAsOf money Default '0' ,
Primary Key(TransactionID)
);
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('123AABDF','Credit','22535.215');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('123AABDF','Debit','215.9');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2256DF','Credit','500');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2256DF','Debit','100');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2351AV','Credit','5000');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2351AV','Debit','100');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('123AABDF','Debit','235.215');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2256DF','Credit','1000');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2351AV','Credit','500');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('7865HNH','Credit','982000');
what I want is that The account having same AccountNumber if the TransactionType is Debit then subtract from BalanceAsOf and if the TransactionType is Credit then adding to BalanceAsOF:
This is what I tried but doesn't work:
update bt
Set BalanceAsOF = case
when AccountNumber=AccountNumber and TransactionType = 'Credit'
then ( BalanceAsOf + TransactionAmount )
when AccountNumber=AccountNumber AND TransactionType='Debit'
then BalanceAsOf - TransactionAmount
End
from
dbo.BankTransaction as bt
Select * From dbo.BankTransaction as p
This is exactly what you should do
set nocount on;
Create table BankTransaction
(
TransactionID int IDENTITY(1,1),
AccountNumber varchar(25) Not Null,
TransactionDate datetime not null Default getdate(),
TransactionType varchar(25) Not Null,
TransactionAmount money Default '0',
BalanceAsOf money Default '0' ,
Primary Key(TransactionID)
);
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('123AABDF','Credit','22535.215');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('123AABDF','Debit','215.9');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2256DF','Credit','500');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2256DF','Debit','100');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2351AV','Credit','5000');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2351AV','Debit','100');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('123AABDF','Debit','235.215');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2256DF','Credit','1000');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2351AV','Credit','500');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('7865HNH','Credit','982000');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('876YYT','Credit','27363647');
Create Table #AccountNomber (
AccountNumber varchar(25) Not Null,);
insert into #AccountNomber ( AccountNumber)
Select Distinct BT.AccountNumber
From dbo.BankTransaction as BT
--select * from #AccountNomber
Declare #PreviousBalance money
Set #PreviousBalance = 0
declare #AcctNumber varchar(25)
declare #TransactionId int
--update b
--set b.BalanceAsOf=0
--from BankTransaction b
while exists(select 1 from #AccountNomber)
begin
select #AcctNumber = a.AccountNumber from #AccountNomber a
--select 'test',* from BankTransaction b
--where b.AccountNumber=#AcctNumber
--and b.BalanceAsOf=0
--order by b.TransactionID
--/*
while exists (select 1 from BankTransaction b where b.AccountNumber=#AcctNumber
and b.BalanceAsOf=0
)
begin
select #TransactionId = (select top 1 b.TransactionID from BankTransaction b
where b.AccountNumber=#AcctNumber
and b.BalanceAsOf=0
order by b.TransactionID)
select #PreviousBalance =#PreviousBalance + case when b.TransactionType='Credit' then b.TransactionAmount else -1*b.TransactionAmount end
from BankTransaction b
Where b.AccountNumber=#AcctNumber
and b.TransactionID=#TransactionId
update b
set b.BalanceAsOf=#PreviousBalance
from BankTransaction b
Where b.AccountNumber=#AcctNumber
and b.TransactionID=#TransactionId
end
select #PreviousBalance =0
delete a from #AccountNomber a
where a.AccountNumber=#AcctNumber
End
select * from BankTransaction b
order by b.TransactionID
Drop table BankTransaction
drop table #AccountNomber
Is there a reason to store the current Balance for each transaction? If you just want to display the balance to user you can have a column in Accounts table for example and for every transaction (insert into BankTransaction) you update this column via trigger on BankTransaction or through your application.
UPDATE
Use a trigger to calculate the balance before inserting the new transaction
CREATE TRIGGER trig_UpdBalance
ON [BankTransaction]
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO BankTransaction
AccountNumber,
TransactionType,
TransactionAmount,
BalanceAsOf
SELECT
i.AccountNumber,
i.TransactionType,
i.TransactionAmount,
CASE
WHEN i.TransactionType = 'Credit'
THEN ( bt.BalanceAsOf + i.TransactionAmount )
WHEN i.TransactionType='Debit'
THEN bt.BalanceAsOf - i.TransactionAmount
END
FROM Inserted i
INNER JOIN BankTransaction bt
ON BT.AccountNumber = i.AccountNumber
AND TransactionID = (SELECT MAX(TransactionID)
FROM BankTransaction
WHERE AccountNumber = i.AccountNumber)
END

SQL Server : procedure tables cannot insert value null

I want to add 100 storages in a table.
This is my procedure:
CREATE PROCEDURE [add100*sTORAGE]
AS
DECLARE #i int, #start DATETIME, #end DATETIME
SET #start = GETDATE()
SET #i = 1
WHILE #i < 101
BEGIN
INSERT INTO Storage(storage_name)
VALUES (CONCAT('Nume', CONVERT(nvarchar, #i)))
SET #i = #i +1
END
SET #end = GETDATE()
DECLARE #testID INT = (SELECT TOP 1 (TestRunID)
FROM TestRuns
ORDER BY TestRunID DESC)
DECLARE #tableID INT = (SELECT tableID
FROM Tables
WHERE Name = 'Storage')
INSERT INTO TestRunTables (TestRunID, TableID, StartAt, EndAt)
VALUES (#testID, #tableID, #start, #end)
GO
I get an error after its execution:
Msg 515, Level 16, State 2, Procedure add100*sTORAGE, Line 13
Cannot insert the value NULL into column 'TestRunID', table 'OnlineShop.dbo.TestRunTables'; column does not allow nulls. INSERT fails.
When I look in the table, it has been created 99 columns.
I have some empty tables in a relation and this are the inserts of it (maybe here is the cause):
--INSERTS--
-- insert views into "Views"
INSERT INTO Views(Name) VALUES ('View1')
INSERT INTO Views(Name) VALUES ('View2')
INSERT INTO Views(Name) VALUES ('View3')
select * from views
delete from views where ViewID>1
-- insert into "Tests"
INSERT INTO Tests(Name) VALUES ('[add100*Storage-runView1-del100*Storage]')
INSERT INTO Tests(Name) VALUES ('[add100*Product-runView2-del100*Product]')
INSERT INTO Tests(Name) VALUES ('[add100*OrderProduct-runView3- del100*OrderProduct]')
SELECT * FROM Tests
--insert into tables
INSERT INTO Tables(Name) VALUES ('Table1')
INSERT INTO Tables(Name) VALUES ('Table2')
INSERT INTO Tables(Name) VALUES ('Table3')
SELECT * from Tables
-- insert into "testTable"
INSERT INTO TestTables(TestID, TableID, NoOfRows, Position) VALUES (1,1,100,1)
INSERT INTO TestTables(TestID, TableID, NoOfRows, Position) VALUES (3,2,100,1)
INSERT INTO TestTables(TestID, TableID, NoOfRows, Position) VALUES (2,3,100,1)
SELECT * FROM TestTables
-- insert into "testViews"
INSERT INTO TestViews(TestID,ViewID) VALUES (1,1)
INSERT INTO TestViews(TestID,ViewID) VALUES (3,2)
INSERT INTO TestViews(TestID,ViewID) VALUES (2,3)
SELECT * FROM TestViews
What's wrong? Thank you.
The error tells you everything--table TestRunTables has column "TestRunID" which requires that field to have a value. You either need to be sure to insert a value into that field, or alter the column so that it will use a default value when you don't specify it.
This line:
DECLARE #testID INT = (SELECT TOP 1 (TestRunID) FROM TestRuns ORDER BY TestRunID DESC)
will set #testID to null if no records are returned from TestRuns or if the first TestRunID is null. This is probably what you need to fix.

SQL QUERY SEARCH EACH WORDS, SPLIT SPACE

question for sql server query.
I try to create a query for searching every word on a sendence. words are split by a space.
The result contain lines that the value contain every word not at least 1.
sample:
DECLARE #DATATABLE TABLE (ID INT,COL1 VARCHAR(MAX))
INSERT INTO #DATATABLE (ID,COL1) VALUES (1,'ALPHA')
INSERT INTO #DATATABLE (ID,COL1) VALUES (2,'ALPHA BETA')
INSERT INTO #DATATABLE (ID,COL1) VALUES (3,'ALPHA BETA CHARLIE')
INSERT INTO #DATATABLE (ID,COL1) VALUES (4,'ALPHA BETA CHARLIE DELTA')
DECLARE #TESTTABLE AS TABLE (ID INT,COL1 VARCHAR(MAX))
INSERT INTO #TESTTABLE (ID,COL1) VALUES (1,'ALPHA') -- RETURN 1,2,3,4
INSERT INTO #TESTTABLE (ID,COL1) VALUES (2,'ALPHA BETA') -- RETURN 2,3,4
INSERT INTO #TESTTABLE (ID,COL1) VALUES (3,'BETA CHARLIE ALPHA') -- RETURN 3,4
INSERT INTO #TESTTABLE (ID,COL1) VALUES (4,'BETA DELTA') -- RETURN 4
INSERT INTO #TESTTABLE (ID,COL1) VALUES (5,'CHARLIE') -- RETURN 3,4
SELECT * FROM #DATATABLE
DECLARE #I AS INT
SET #I = 1
DECLARE #COUNTLINE AS INT
SET #COUNTLINE = (SELECT MAX(ID) FROM #TESTTABLE )
DECLARE #CURRENTVALUE AS VARCHAR(MAX)
WHILE(#I <= #COUNTLINE)
BEGIN
SET #CURRENTVALUE = (SELECT COL1 FROM #DATATABLE WHERE ID = #I)
-- ** TODO **
-- QUERY SHOULD RETURN EVERY LINE CONTAINING EACH WORD OF THE CURRENT TEST VALUE
SELECT * FROM #DATATABLE WHERE COL1 LIKE #CURRENTVALUE
SET #I = #I + 1
END
I write "TODO" where I put the first test (saw what that shouldn't do).
I cant create SP or Function.
I look on my side for "with" and "in" or something else.
tank for any tips
and good week end