SQL Server Procedures Questions

Hello,

 

I needed some simple help in SQL Server, given below are my two questions.

I am looking at making a SQL Server Procedure which uses cursors to detect some data entry 

mistakes. 

1) I have a query joining two or three tables and returning me a list of rows. I would like to compare the Name column in this list using a like operator and report back any names which are similar. How can this be done ? If possible please send me a brief skeletal code to do the same.

2) I am using a query to join names of employees with languages they speak. The query works fine but for employees who speak more than 1 language, the record gets repeated with a new line for every new language they speak, instead of doing this, I would like to concatenate their languages in 1 column and report back 1 row per employee. Is this possible ?

If yes please send me the brief skeletal code to do this on faizq@yahoo.com

Thanks

Irfan

February 22nd, 2015 7:43pm

Sample code to do part 2) of your question

Create Table #Employees(EmployeeName varchar(50), Constraint EPK Primary Key(EmployeeName));
Create Table #Languages(LanguageName varchar(20), Constraint LPK Primary Key(LanguageName));
Create Table #EmployeeLanguages(EmployeeName varchar(50), LanguageName varchar(20), Constraint ELPK Primary Key(EmployeeName, LanguageName));
Insert #Employees(EmployeeName) Values
('John Doe'), ('Mary Smith'), ('Tom Jones');
Insert #Languages(LanguageName) Values
('English'), ('Arabic'), ('French'), ('Chinese');
Insert #EmployeeLanguages(EmployeeName, LanguageName) Values
('John Doe', 'English'),
('John Doe', 'French'),
('Mary Smith', 'English'),
('Tom Jones', 'French'),
('Tom Jones', 'Arabic'),
('Tom Jones', 'Chinese');

Select Distinct e.EmployeeName,
    (Select Stuff((Select ', ' + l.LanguageName 
	  From #Languages l
	  Inner Join #EmployeeLanguages el On e.EmployeeName = el.EmployeeName And l.LanguageName = el.LanguageName
	     For XML Path(''),Type)
    .value('text()[1]','nvarchar(max)'),1,2,N'')) As Languages
From #Employees e
Order By e.EmployeeName;

Go
Drop Table #Employees;
Go
Drop Table #Languages;
Go
Drop Table #EmployeeLanguages;
Tom
Free Windows Admin Tool Kit Click here and download it now
February 22nd, 2015 9:34pm

>> If yes please send me the brief skeletal code to do this on faizq@yahoo.com

Good day Irfan

This is not a paid supporting center, where you get private support, by commercial company, but a public forum where communities members helps each other. Public posts, answers, and discussions have a huge advantage! Other people might learn from the thread as well.

>> I have a query joining two or three...

Please check Tom Cooper's answer, and if this is not what you are looking for, then please post more information and less stories (I mean codes). Please show us the query instead of describe it. the code describe it better :-) Moreover, pls post DDL+DML


February 23rd, 2015 3:21am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics