Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have written two functions exactly doing the same thing on two SQL tables containing some item information. Removing the unwanted characters and adding on the entries of the first table and creating a translation key as item.groups and on the second table item.class to the result set. My aim is to create a single function doing my job.

First table contain:
itemgroups
group_name
fertilizer1
fertilizer2

Second table
item
group_name | name
fertilizer1 | nitrogen
fertilizer1 | phosphorus
fertilizer2 | potassium
fertilizer2 |sodium




<pre><pre>ALTER  FUNCTION [dbo].[new_translation_groups]
(  
   @column_name nvarchar(1000),
   @group_name nvarchar(1000)
) 
Returns nvarchar(1000)  
As
BEGIN
DECLARE @result nvarchar(1000);
DECLARE @result2 nvarchar(1000);
    
    SET @result = @column_name;
	SET @result2 = @group_name;

	SET @result2 = Replace(@result2, N' OR ', N'_');
	SET @result2 = Replace(@result2, N' and ', N'_');
    SET @result2 = Replace(@result2, N' ', N'_');
	SET @result2 = Replace(@result2, N'/', N'_');
	
	SET @result = Replace(@result, N' OR ', N'_');
	SET @result = Replace(@result, N' and ', N'_');
    SET @result = Replace(@result, N' ', N'_');
	SET @result = Replace(@result, N'-', N'_');

	


    
    WHILE @result Like N'%[_][_]%'
    BEGIN
        SET @result = Replace(@result, N'_', N'');
    END;

	 WHILE @result2 Like N'%[_][_]%'
    BEGIN
        SET @result2 = Replace(@result, N'_', N'');
    END;

	

	RETURN N'item.class.'+LOWER(@result2)+ N'_'+ LOWER(@result);
END;






My second function
CREATE OR ALTER FUNCTION dbo.translationkey_v22
(  
   @column_name nvarchar(1000) 
) 
Returns nvarchar(1000)  
As
BEGIN
DECLARE @result nvarchar(1000);
    
    SET @result = @column_name;
    SET @result = Replace(@result, N'and', N'_');
    SET @result = Replace(@result, N' ', N'_');
    
    WHILE @result Like N'%[_][_]%'
    BEGIN
        SET @result = Replace(@result, N'__', N'');
    END;
    
    Return N'item.group.' + LOWER(@result);
END;


What I have tried:

what i have tried


Alter FUNCTION [dbo].[new_translation_groups_newV1]
(
@column_name nvarchar(1000),
@group_name nvarchar(1000)

)
Returns nvarchar(1000)
As
BEGIN
DECLARE @result nvarchar(1000);
DECLARE @result2 nvarchar(1000);
DECLARE @result3 nvarchar(1000);

SET @result = @column_name;
SET @result2 = @group_name;

SET @result2 = Replace(@result2, N' OR ', N'_');
SET @result2 = Replace(@result2, N' and ', N'_');
SET @result2 = Replace(@result2, N' ', N'_');
SET @result2 = Replace(@result2, N'/', N'_');

SET @result = Replace(@result, N' OR ', N'_');
SET @result = Replace(@result, N' and ', N'_');
SET @result = Replace(@result, N' ', N'_');
SET @result = Replace(@result, N'-', N'_');





WHILE @result Like N'%[_][_]%'
BEGIN
SET @result = Replace(@result, N'_', N'');
END;

WHILE @result2 Like N'%[_][_]%'
BEGIN
SET @result2 = Replace(@result, N'_', N'');
END;


IF (@group_name = (SELECT group_name from itemGroups))
SET @result3 = N'item.group.' + LOWER(@result)
ELSE IF (@group_name = (SELECT group_name from item))
SET @result3= N'item.class.'+LOWER(@result2)+ N'_'+ LOWER(@result);

RETURN @result3

END;


But this is giving error as the query is selecting multiple rows
Posted
Updated 14-Sep-21 16:49pm
v2
Comments
OriginalGriff 14-Sep-21 11:58am    
And?
What have you tried?
Where are you stuck?
What help do you need?

Use the "Improve question" widget to edit your question and provide better information.

1 solution

Looking at this part of code
SQL
IF (@group_name = (SELECT group_name from itemGroups))
SET @result3 = N'item.group.' + LOWER(@result)
ELSE IF (@group_name = (SELECT group_name from item))
SET @result3= N'item.class.'+LOWER(@result2)+ N'_'+ LOWER(@result);

The SELECT statement in your IF does not have any WHERE clause. This means that all the rows from itemGroups are returned. On the other hand equality comparison can only be done for individual values hence the error

You haven't explained what you're trying to do in the comparison but if you want to check if the group name exists in itemGroups you could try something like

SQL
IF EXISTS (SELECT 1 FROM ItemGroups WHERE Group_Name = @group_name) BEGIN
   SET @result3 = N'item.group.' + LOWER(@result)
END 
ELSE IF EXISTS (SELECT 1 FROM item WHERE Group_Name = @group_name) BEGIN
   SET @result3 = N'item.class.'+LOWER(@result2) + N'_'+ LOWER(@result);
END
 
Share this answer
 
Comments
Member 15352290 15-Sep-21 2:59am    
Hi actually I want to write a single function for both these tables mentioned above and based on some conditions select one table and create a new column with translation added as item.group and if another table is selected all the item should be added with item.class. The only thing i am not understanding is how to use a single function doing this functionality for both these tables
Member 15352290 15-Sep-21 3:03am    
Thank you so much for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900