16.02.2018
0 Yorum
SQL Dinamik Sorgu Örnek ;
DECLARE @SQL NVARCHAR(MAX) -- SORGU SONUCUNU ALACAGIMIZ DEĞİŞKEN
DECLARE @PARAMETRE1 NVARCHAR(50)
BEGIN TRY BEGIN TRANSACTION SET @SQL = ' SELECT * FROM TABLO '+@PARAMETRE1+' '
PRINT @SQL EXEC sys.sp_executesql @SQL COMMIT TRANSACTION END TRY BEGIN CATCH PRINT 'Error ' + CONVERT(varchar(50), ERROR_NUMBER()) + ', Severity ' + CONVERT(varchar(5), ERROR_SEVERITY()) + ', State ' + CONVERT(varchar(5), ERROR_STATE()) + ', Line ' + CONVERT(varchar(5), ERROR_LINE()) PRINT ERROR_MESSAGE(); IF XACT_STATE() <> 0 BEGIN ROLLBACK TRANSACTION END END CATCH;