Stack Exchange
Transforming SQL statement - no cursors and loops needed?
I have got the following SQL statement (SQL Server):
DECLARE @atr nvarchar(255), @con nvarchar(1000), @func nvarchar(5);
SET @con='example'
SET @func=(SELECT FNCELEM FROM CNDSC WHERE NAME LIKE @con)
DECLARE Atr_Cursor CURSOR FOR
SELECT ATRBT FROM CNEA WHERE (ELEMS LIKE '%'+@func+'%' AND NOT ELEMS LIKE '%1' + @func   + '%');
OPEN Atr_Cursor;
FETCH NEXT FROM Atr_Cursor
INTO @atr;
WHILE @@FETCH_STATUS = 0
  BEGIN
    DECLARE @SQLString nvarchar(500);
    IF (SELECT COUNT(*) FROM CNEXTRA WHERE ATR LIKE ''+@atr+'' AND NAME LIKE  ''+@con+'')>0
    BEGIN
    SET @SQLString= N'SELECT NAME, ATR, CNVAL, INRDR FROM CNEXTRA WHERE ATR LIKE ''' + @atr + ''' AND NAME LIKE '''+@con+''';';
    END
    ELSE
    BEGIN
    SET @SQLString= N'SELECT '''+@con+''' AS NAME, '''+@atr+''' AS ATR, '''' AS CNVAL, '''' AS INRDR';
    END
    EXEC (@SQLString);
    FETCH NEXT FROM Atr_Cursor
    INTO @atr;
  END
CLOSE Atr_Cursor;
DEALLOCATE Atr_Cursor;

Obviously, this statement returns several results due to the loop. While trying to use this statement with my C# code, I can only get the result of the last loop (obviously too). So I tried to transer the results into a temporary table; that worked. But now I've got the problem that I am not able to build the "complement" of that statement, so that the results are written back into the table.
If it is possible to change the query, so that there are no more loops, I think building the complement of the query will be easier.
So in fact my question is: Is there a chance to change the statement, so that there are no more loops? How could that look like? And if it's not possible, how will I have to build a complement of that statement?
Thanks a lot for any help.

I think this query will do it.
SELECT
    CNDSC.NAME,
    CNEA.ATRBT AS ATR,
    ISNULL(CNEXTRA.CNVAL,'') AS CNVAL,
    ISNULL(CNEXTRA.INRDR,'') AS INRDR
FROM
    CNDSC
    INNER JOIN CNEA
        ON CNEA.ELEMS LIKE '%'+CAST(CNDSC.FNCELEM AS VARCHAR)+'%' AND
           NOT CNEA.ELEMS LIKE '%1'+CAST(CNDSC.FNCELEM AS VARCHAR)+'%'  
    LEFT OUTER JOIN CNEXTRA
        ON CNEXTRA.ATR LIKE CNEA.ATRBT AND
           CNEXTRA.NAME LIKE @con
WHERE
    CNDSC.NAME LIKE @con;

I used this schema.  Please let me know which columns are wrong.
CREATE TABLE CNDSC
    (NAME  VARCHAR(100), FNCELEM  VARCHAR(100));
INSERT INTO CNDSC VALUES ('ONE','FUNC1');
INSERT INTO CNDSC VALUES ('TWO','FUNC2');

CREATE TABLE CNEA
    (ATRBT VARCHAR (100), ELEMS VARCHAR(100));
INSERT INTO CNEA VALUES ('ATTRIBUTE1','FUNC1');
INSERT INTO CNEA VALUES ('ATTRIBUTE2','FUNC2');

CREATE TABLE CNEXTRA
    (ATR VARCHAR(100),
     NAME VARCHAR(100),
     CNVAL  VARCHAR(100),
     INRDR  VARCHAR(100));
INSERT INTO CNEXTRA VALUES ('ATTRIBUTE1','ONE','CNVAL','INRDR');


Unfortunately I get an error... "Conversion failed when converting the varchar value '%' to data type int"

I assumed that all of the columns referenced in your queries were of type varchar.  If any are not please let me know.

I've added CAST to CNDSC.FNCELEM in case that's a numeric column.

It's CNDSC.FNCELEM that seems to make the problem. When converting the fncelem to a char [ char(CNDSC.FNCELEM) ], the error is gone, but the result is empty

char() doesn't convert to the number to a string containing that number.   eg:  char(123) is not '13'
instead it returns the character with that code.  so char(13) is newline.

I've added now CAST(CNDSC.FNCELEM AS VARCHAR), but the result is still empty.
