Sql server: how to get the value from a sql string -


this sql statement not work. after exec (@strsql), see 0 or 1 result continue if statement doesn't work expected. doesn't take action if statement. also, there way make query dynamic check more 1 table?

declare @strsql varchar(1000), @tablename varchar(100), @linkedserver varchar(100)  set @tablename='tablename' set @linkedserver='linkservername' set @strsql='select count(1) tabexists dbc.tables       tablekind=''t'' , databasename=''databasename'' ,        tablename=''tablename''' set  @strsql = n'select tabexists openquery('+@linkedserver+', ''' + replace(@strsql, '''', '''''') + ''')' exec (@strsql)  if @strsql = '0'  --table not exist     create table if @strsql = '1'  --table exist delete data table 

instead try

declare @sql nvarchar(max) = '' declare @tabexists bit;  set @strsql = 'select @tabexists = case when tabexists = 0 0 else 1 end         openquery(' + quotename(@linkedserver)          + ', ''select tabexists = count(*)                  linkeddb.information_schema.tables                 table_name = ' + @tablename + ');';  execute sp_executesql @strsql, n'@tabexists bit output', @tabexists out;  if (@tabexists = 0) begin     -- create table end; 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -