Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
DECLARE @t_sup TABLE (
sup_id int,
login_name varchar(128),
sup_level varchar(128),
process_code varchar(128)
)

--DECLARE @contact_id AS NVARCHAR(50); SET @contact_id = '7984';
--DECLARE @imp_id AS NVARCHAR(50); SET @imp_id = '202548';

INSERT INTO @t_sup (sup_id, login_name)
SELECT s.sup_id, COALESCE(ls.login_name, 'root')
FROM t_sup_supplier s
LEFT JOIN t_usr_login_securable ls ON ls.sec_id=s.sec_id AND ls.profil_code='sup_owner'
LEFT JOIN t_wfl_process_execution pe ON pe.tdesc_name='t_sup_supplier' AND pe.x_id=CAST(s.sup_id AS varchar(128))
WHERE s.imp_id=@imp_id --AND pe.process_code IS NULL
SELECT * FROM @t_sup

UPDATE ts
SET sup_level=l.lvl_code
FROM @t_sup ts
INNER JOIN (
SELECT ts.sup_id, MAX(lvl_order) AS min_lvl_order
FROM @t_sup ts
INNER JOIN t_sup_supplier_level sl ON sl.sup_id=ts.sup_id
INNER JOIN t_pbi_level l ON l.lvl_code=sl.lvl_code
GROUP BY ts.sup_id
) AS ml ON ml.sup_id=ts.sup_id
INNER JOIN t_pbi_level l ON l.lvl_order=ml.min_lvl_order AND l.dim_code='sup'

-- Déc
DECLARE @process_code AS VARCHAR(50)

SELECT @contact_id = COALESCE(usr.contact_id, 1)
FROM t_sup_supplier s
INNER JOIN t_usr_login as usr ON usr.login_name = s.login_name_created
WHERE s.imp_id=@imp_id

SELECT @process_code = process_code FROM t_wfl_process_x WHERE tdesc_name = 't_sup_supplier' --AND process_code is not null

-- Mise à jour WFL SUPPLIER V2 - Etape Création (INI)
IF NOT EXISTS (SELECT * FROM t_wfl_worklist wfl INNER JOIN t_sup_supplier s on s.sup_id = wfl.x_id WHERE 1=1 AND wfl.process_code = 'SUPPLIER_V2' AND wfl.act_code = 'INI' AND wfl.x_id = s.sup_id AND s.imp_id=@imp_id)
INSERT INTO t_wfl_worklist(process_code, x_id, tdesc_name, act_code, contact_id_performer,wli_date_ini, wli_date_val, wli_comment)
SELECT @process_code, s.sup_id, 't_sup_supplier', 'INI', @contact_id, GETDATE(), GETDATE(), @contact_id+GETDATE()
FROM dbo.t_sup_supplier AS s INNER JOIN t_wfl_worklist wfl on wfl.x_id = s.sup_id WHERE s.imp_id=@imp_id AND contact_id_performer = @contact_id AND process_code = 'SUPPLIER_V2'--AND process_code is not null
ELSE UPDATE t_wfl_worklist SET contact_id_performer = @contact_id, wli_date_val = GETDATE(), wli_comment = @contact_id+GETDATE()
FROM t_wfl_worklist INNER JOIN t_sup_supplier as s on s.sup_id=t_wfl_worklist.x_id WHERE x_id = s.sup_id AND act_code = 'INI' AND s.imp_id= @imp_id AND contact_id_performer = @contact_id

-- Mise à jour WFL SUPPLIER V2 - Etape Validation groupe (RSK)
IF NOT EXISTS (SELECT * FROM t_wfl_worklist wfl INNER JOIN t_sup_supplier s on s.sup_id = wfl.x_id LEFT JOIN t_sup_supplier_level lvl on s.sup_id=lvl.sup_id
WHERE 1=1 AND process_code = 'SUPPLIER_V2' AND act_code = 'RSK' AND x_id = s.sup_id AND s.imp_id=@imp_id AND lvl.lvl_code <> 'ult')

What I have tried:

i tried to debug myself, but... i can't
Posted
Updated 20-Jan-20 0:31am
Comments
Santosh kumar Pithani 29-Aug-18 0:14am    
How can we find problem in this query?Its a conversion problem make sure which datatypes are using for join condition either provide some testing data.
Naga Sindhura 29-Aug-18 3:58am    
LEFT JOIN t_wfl_process_execution pe ON pe.tdesc_name='t_sup_supplier' AND pe.x_id=CAST(s.sup_id AS varchar(128))

what is the datatype of pe.x_id column int/varchar. If it is int then why you are converting and then comparing it with s.sup_id(CAST(s.sup_id AS varchar(128))).
or some where in the data, you trying to convert varchar data which contains some sort of characters and you are converting that data to int.

1 solution

Fix your data first, then look at optimising queries.
The error is saying "I can't convert that - it isn't a number in string form" - so you need to look more closely at the error report, and find out which line and column of the query it is complaining about.
When you have that, you can find out what value it is trying to convert, and then work back from that to where the data it is trying to handle comes from.
If it's from a DB, then why are you storing numeric values in a text field? Change your DB so that you don't.
If it's from user input, why aren't you validating your inputs before you start passing them to SQL? Change your presentation software to always validate and pass useful data in the correct datatypes via parameterised queries only. (Because this implies that your presentation software may well be concatenating strings to produce SQL queries, and that's very dangerous: look up SQL Injection)
If it's from another source, where is it? What should it be passing? Why isn't it?

We can;lt do any of that for you - it requires access to your DB and the erroring dat, and we have neither.


And when it comes to optimising your queries, dumping it here with commented out code and no actual attempt to indent or structure the query helps no-one - I'm certainly not going to even try to work out what that is supposed to be doing, much less how it does it at present - again, we have no access to your data so we can't run it against actual information. So try looking at SQL's execution plan and see what that says about the query and where any bottlenecks might be.
 
Share this answer
 
Comments
CHill60 29-Aug-18 8:31am    
1-vote countered ... not because you need the rep points (:-) ) but because other readers need to know that this is a Good Answer!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900