Hi AllI'm trying to create a UDF, and my TSQL skills when it comes to Cursors is rustier that I'd like!The idea of the function is essentially this:1. A supplied parameter of @ODID identifies an Order Detail ID. This is used to look up from 0 - n matching InvoiceIDs. These are retrieved by means of a cursor, which may return 0 rows (@InvoiceIDs is populated with an empty string), 1 row (@InvoiceIDs is populated with that one InvoiceID) or more than one row (@InvoiceIDs is populated with a comma-delimited set of InvoiceID values).2. A second cursor is created to look up a "P number" (a reference to a paper file used by my client), incorporating the WHERE clause ' WHERE lngInvoiceID IN (@InvoiceIDs)'.At least that's the theory. But, if there are no matching invoices, I get the null return that I expect. If there is one matching invoice (and P number) I still get Null. And if there are multiple matching invoices (and P Numbers) then I get the error "Conversion failed when converting the varchar value '5366,5829,6442' to data type int." (These are the Invoices that match the Order Detail ID I supplied in this case).I hope that makes sense - I'm pasting the entire UDF below in case that helps more. I'd be really grateful for any help anyone can offer me...Thanks a lotAndrewALTER FUNCTION [dbo].[pnumsforodid]( @OdID INT ) RETURNS VARCHAR(1024) ASBEGIN DECLARE @InvoiceIDs varchar(1024), @invoiceID varchar(6),@InvoiceCount int DECLARE @PnumList varchar(1024), @PNUM varchar(8), @PNUMCOUNT INT DECLARE @InvoiceList CURSOR SET @InvoiceList = CURSOR STATIC FOR SELECT lngInvoiceID FROM manufacturing.tblInvoiceOrderDetails WHERE lngOrderDetailID = @OdID; OPEN @InvoiceList SET @InvoiceCount= @@CURSOR_ROWS IF @InvoiceCount=0 BEGIN SET @PnumList='' SET @InvoiceIDs='' END ELSE BEGIN IF @InvoiceCount=1 BEGIN FETCH NEXT FROM @InvoiceList INTO @InvoiceIDs END ELSE --InvoiceCount > 1 BEGIN FETCH NEXT FROM @InvoiceList INTO @InvoiceID WHILE @@Fetch_Status=0 BEGIN SET @InvoiceIDs=convert(varchar(1024),isnull(@InvoiceIDs,'')) + ',' + convert(varchar(10),@InvoiceID) FETCH NEXT FROM @InvoiceList INTO @InvoiceID END END END CLOSE @InvoiceList DEALLOCATE @InvoiceList IF Len(@InvoiceIDs) > 0 BEGIN SET @InvoiceIDs = RIGHT(@InvoiceIDs, Len(@InvoiceIDs) - 1) DECLARE @PNUMLISTSET CURSOR SET @PNumListSet=CURSOR STATIC FOR Select strPNumber from finance.tblInvoices where lngInvoiceID in (@InvoiceIDs) --This is the line that generates the type conversion error and strPNumber is not null OPEN @PNumListSet SET @PNUMCOUNT=@@CURSOR_ROWS IF @PNUMCOUNT=0 SET @PnumList='' ELSE BEGIN IF @PNUMCOUNT=1 BEGIN FETCH NEXT FROM @PNUMLISTSET INTO @PnumList END ELSE BEGIN FETCH NEXT FROM @PNUMLISTSET INTO @PNUM WHILE @@Fetch_Status=0 BEGIN SET @PnumList=convert(varchar(1024),isnull(@PnumList,'')) + ',' + convert(varchar(1024),@PNUM) FETCH NEXT FROM @PNUMLISTSET INTO @PNUM END SET @PnumList = RIGHT(@PnumList, Len(@PnumList) - 1) END END CLOSE @PNumListSet DEALLOCATE @PNumListSet END RETURN nullif(@PnumList,'')END