July 16, 2017 at 1:17 pm
Hi
I want to write if the table exists in the database in dynamic sql then select from that table .
I have written this but i want to use in if then
DECLARE @sql NVARCHAR(MAX);
set @sql = 'SELECT * FROM ['+ @DBID+'].sys.objects where NAME LIKE ''%DDSC_MappedDimension%'' '
SET @RunTimeQuery = 'SELECT distinct MAPPEDDIMENSIONCODE,DIMENSION_TYPE FROM ['+ @DBID+'].dbo.DDSC_MappedDimension WHERE MAPPEDDIMENSIONCODE = ''' + @code + '''' + 'and DIMENSION_TYPE = ''' + @Dimension_Type +''''
July 16, 2017 at 2:03 pm
mnr123 - Sunday, July 16, 2017 1:17 PMHiI want to write if the table exists in the database in dynamic sql then select from that table .
I have written this but i want to use in if then
DECLARE @sql NVARCHAR(MAX);
set @sql = 'SELECT * FROM ['+ @DBID+'].sys.objects where NAME LIKE ''%DDSC_MappedDimension%'' 'SET @RunTimeQuery = 'SELECT distinct MAPPEDDIMENSIONCODE,DIMENSION_TYPE FROM ['+ @DBID+'].dbo.DDSC_MappedDimension WHERE MAPPEDDIMENSIONCODE = ''' + @code + '''' + 'and DIMENSION_TYPE = ''' + @Dimension_Type +''''
Your logic sounds pretty straightforward.
DECLARE @Name varchar(128) = 'dbo.Test';
DECLARE @sql Varchar(200);
IF OBJECT_ID(@Name, 'U') IS NOT NULL
BEGIN
--TODO: define your query here
EXECUTE (@SQL);
END;
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply