Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
immad
Posting Yak Master
230 Posts |
Posted - 2013-12-19 : 04:07:26
|
hello i am working on this queryALTER FUNCTION [dbo].[employeephoneextensions](@CID int,@BID int,@EmpID int,@DesignationID varchar,@Did VARCHAR)RETURNS TABLEASRETURN SELECTT.CID,T2.CName,T.BID,T3.Bname,T.EID,T.Ename,T1.dname Deparmtent,T.Extension,CASE WHEN T.DirectNo is null THEN '-' ELSE T.DirectNo END as DirectNo,CASE WHEN T.CellNo is null THEN '-' ELSE T.CellNo END as CellNo,T4.name [Designation],T.Gradefrom employee Tleft outer join department T1 on T.CID = T1.CID AND T.BID = T1.BID AND T.did = T1.didleft outer join Company T2 on T.CID = T2.CIDleft outer join Branch T3 on T.CID = T3.CID AND T.BID = T3.BIDleft outer join Designation T4 on T.CID = T4.CID AND T.BID = T4.BID AND T.designationid = T4.designationidwhere T.leavingdate is null AND t.extension is not nullAND (ISNULL(@CID,'')='' OR T.CID = @CID)AND (ISNULL(@BID,'')='' OR T.BID = @BID)AND (ISNULL(@EmpID,'')='' OR T.EID = @EmpID)AND (ISNULL(@DesignationID,'')='' OR ',' + @DesignationID + ',' LIKE '%,' + CAST(T.designationid AS varchar) + ',%')AND (ISNULL(@Did,'')='' OR ',' + @Did + ',' LIKE '%,' + CAST(T.DID AS varchar) + ',%')i have problem .the problem is that when i write below statmentselect * from [employeephoneextensions] ('1','2','26589','','214')this give me null data.but In database data is availablemay be there is a problem after where clauseplease help me outthanks in advanceimmad uddin ahmed |
|
Kristen
Test
22859 Posts |
Posted - 2013-12-19 : 04:32:11
|
You have:ISNULL(@CID,'')='' but @CID is an INT ??andCAST(T.DID AS varchar)CAST(T.designationid AS varchar) you should include a size to the VARCHAR. Presumably you are just converting numbers, so the default size is probably OK, but its a had habit IMHO.That means of making a wild-card match is very inefficient, so will not scale well as the table sizes grow.(On that basis personally I wouldn't be writing this as a Function either, inline code, or a Stored Procedure, is likely to be more efficient, but there may be other considerations that you face) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-19 : 04:32:36
|
The first obvious issue i find with code is you've not specified length for varchar fields. make it like belowALTER FUNCTION [dbo].[employeephoneextensions](@CID int,@BID int,@EmpID int,@DesignationID varchar(20),@Did VARCHAR(20))RETURNS TABLEASRETURN SELECTT.CID,T2.CName,T.BID,T3.Bname,T.EID,T.Ename,T1.dname Deparmtent,T.Extension,CASE WHEN T.DirectNo is null THEN '-' ELSE T.DirectNo END as DirectNo,CASE WHEN T.CellNo is null THEN '-' ELSE T.CellNo END as CellNo,T4.name [Designation],T.Gradefrom employee Tleft outer join department T1 on T.CID = T1.CID AND T.BID = T1.BID AND T.did = T1.didleft outer join Company T2 on T.CID = T2.CIDleft outer join Branch T3 on T.CID = T3.CID AND T.BID = T3.BIDleft outer join Designation T4 on T.CID = T4.CID AND T.BID = T4.BID AND T.designationid = T4.designationidwhere T.leavingdate is null AND t.extension is not nullAND (ISNULL(@CID,'')='' OR T.CID = @CID)AND (ISNULL(@BID,'')='' OR T.BID = @BID)AND (ISNULL(@EmpID,'')='' OR T.EID = @EmpID)AND (ISNULL(@DesignationID,'')='' OR ',' + @DesignationID + ',' LIKE '%,' + CAST(T.designationid AS varchar) + ',%')AND (ISNULL(@Did,'')='' OR ',' + @Did + ',' LIKE '%,' + CAST(T.DID AS varchar(20)) + ',%') Not sure whether thats cause for issue you're facing thoughFor that we might need some details on rows which you feel to be returned like their values for columns which were used in filterhttp://visakhm.blogspot.in/2010/02/importance-of-specifying-length-in.html------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
immad
Posting Yak Master
230 Posts |
Posted - 2013-12-20 : 02:43:38
|
this is not working i did it varchar but still no resultimmad uddin ahmed |
 |
|
Kristen
Test
22859 Posts |
Posted - 2013-12-20 : 04:16:16
|
Sample data, and expected results, please |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-21 : 00:50:00
|
quote: Originally posted by immad this is not working i did it varchar but still no resultimmad uddin ahmed
Unless you give us some sample data showing values you're passing and then explain us the expected output vs your current output we wont be able to help you further.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|
|
|