I have a problem about T-SQL Dynamic SP. The first block is my failure attempt, and the second is my target (non-dynamic block).
CREATE PROCEDURE UDP1_TRY_EQQQ
(@FACTUTABLE nvarchar(100))
AS
declare @sqlquery as nvarchar(100)
BEGIN
SET @sqlquery = 'DELETE FROM ' + @FACTUTABLE + ' WHERE ANEXO IN
(SELECT ANEXO FROM ' + @FACTUTABLE + ' A LEFT JOIN ALTAS_MOVILES B
ON A.ANEXO=B.TCNFOL WHERE B.TCNFOL IS NULL)'
EXEC sp_ExecuteSql @sqlquery
END
DELETE FROM FACTURACION_201210
WHERE ANEXO IN
(
SELECT ANEXO
FROM FACTURACION_201210 A
LEFT JOIN ALTAS_MOVILES B
ON A.ANEXO=B.TCNFOL
WHERE B.TCNFOL IS NULL
)
You only allowed 100 characters for your @sqlquery variable. Make it bigger.
Also, in these dynamic queries where you need to substitute a table name, I like to take two additional precautions:
@sql = 'Select * FROM ' + QuoteName(@table);SELECT Table_Name FROM Information_schema.Tables WHERE Table_Name = @FACTUTABLE;If you want to get real fancy, you can also check that the table is in the right schema and even check within information_schema.columns to make sure the chosen table has the right column names, but normally just these first two checks is as far as I go.