sql server - Transforming SQL statement - no cursors and loops needed? -



sql server - Transforming SQL statement - no cursors and loops needed? -

i have got next sql statement (sql server):

declare @atr nvarchar(255), @con nvarchar(1000), @func nvarchar(5); set @con='example' set @func=(select fncelem cndsc name @con) declare atr_cursor cursor select atrbt cnea (elems '%'+@func+'%' , not elems '%1' + @func + '%'); open atr_cursor; fetch next atr_cursor @atr; while @@fetch_status = 0 begin declare @sqlstring nvarchar(500); if (select count(*) cnextra atr ''+@atr+'' , name ''+@con+'')>0 begin set @sqlstring= n'select name, atr, cnval, inrdr cnextra atr ''' + @atr + ''' , name '''+@con+''';'; end else begin set @sqlstring= n'select '''+@con+''' name, '''+@atr+''' atr, '''' cnval, '''' inrdr'; end exec (@sqlstring); fetch next atr_cursor @atr; end close atr_cursor; deallocate atr_cursor;

obviously, statement returns several results due loop. while trying utilize statement c# code, can result of lastly loop (obviously too). tried transer results temporary table; worked. i've got problem not able build "complement" of statement, results written table.

if possible alter query, there no more loops, think building complement of query easier.

so in fact question is: there chance alter statement, there no more loops? how like? , if it's not possible, how have build complement of statement?

thanks lot help.

i think query it.

select cndsc.name, cnea.atrbt atr, isnull(cnextra.cnval,'') cnval, isnull(cnextra.inrdr,'') inrdr cndsc inner bring together cnea on cnea.elems '%'+cast(cndsc.fncelem varchar)+'%' , not cnea.elems '%1'+cast(cndsc.fncelem varchar)+'%' left outer bring together cnextra on cnextra.atr cnea.atrbt , cnextra.name @con cndsc.name @con;

i used schema. please allow me know columns wrong.

create table cndsc (name varchar(100), fncelem varchar(100)); insert cndsc values ('one','func1'); insert cndsc values ('two','func2'); create table cnea (atrbt varchar (100), elems varchar(100)); insert cnea values ('attribute1','func1'); insert cnea values ('attribute2','func2'); create table cnextra (atr varchar(100), name varchar(100), cnval varchar(100), inrdr varchar(100)); insert cnextra values ('attribute1','one','cnval','inrdr');

sql sql-server

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -