Note that there are some explanatory texts on larger screens.

plurals
  1. POCross-database query with database name in variable and cursor
    primarykey
    data
    text
    <p>I have a problem with name of database in a cursor. Here the current code</p> <pre><code>DECLARE @IDES INT DECLARE @IDPROD INT DECLARE @count INT SET @count = 0 DECLARE CUR_CONSO CURSOR LOCAL fast_forward FOR SELECT E2.id_es ,P3.id_prod FROM [gpto_v004p001].[dbo].[GPTO_PRODETAB] P1 INNER JOIN [fer_v008].[dbo].[T_PRODUIT] P3 ON P3.GPTO_PRODUIT_ID = P1.GPTO_PRODUIT_ID INNER JOIN [gpto_v004p001].[dbo].[GPTO_ETAB] E1 ON E1.ETABID = P1.ETABID INNER JOIN [fer_v008].[dbo].[t_etablissement] E2 ON E1.ETABUC = LEFT(E2.code_es,5) LEFT JOIN [fer_v008].[dbo].[t_produit_etablissement] PE1 ON PE1.id_prod = P3.id_prod AND PE1.id_es = E2.id_es WHERE PE1.id_es IS NULL AND GPTO_PRODUIT_ETAPE = 4 OPEN CUR_CONSO FETCH CUR_CONSO INTO @IDES , @IDPROD WHILE @@FETCH_STATUS = 0 BEGIN IF NOT EXISTS (Select * from [fer_v008].[dbo].[t_produit_etablissement] where id_es=@IDES and id_prod=@IDPROD) -- Pas d'enregistrements BEGIN INSERT INTO [fer_v008].[dbo].[t_produit_etablissement] ([id_es],[id_prod],[gest_prod]) VALUES (@IDES,@IDPROD,0) SET @count = @count + 1 END FETCH CUR_CONSO INTO @IDES , @IDPROD END CLOSE CUR_CONSO DEALLOCATE CUR_CONSO </code></pre> <p>As the database is versionned, I need to use database name as variable saved in parameter table. For easy script, I use Execute command as this</p> <pre><code>DECLARE @base_travail varchar(128) SELECT @base_travail = val_str_par FROM t_parametre WHERE nom_par = 'base_travail' DECLARE @execcmd varchar(max) SET @execcmd = 'insert into #tmpfiltres SELECT TOP 1 filtre_exu FROM ' + @base_travail + '.dbo.t_export_util WHERE id_exu =' + convert(varchar,@id_exu) Execute (@execcmd) </code></pre> <p>But how do this when I have a cursor ? The 1st sample code is just a sample, whole script go over 400 lines, so I can't switch all the script in string mode.</p> <p>Thanks for your help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload