SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 
        
Home       Members    Calendar    Who's On



Array Processing - CLR or TSQL? Expand / Collapse
Author
Message
Posted Tuesday, May 27, 2008 9:10 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 9:28 AM
Points: 228, Visits: 280
I was considering using CLR to write a table-valued function that can accept an array argument and return the elements as a table. I cannot work out a way of achieving what I need using TSQL but maybe you do?!

Thanks
Post #506986
Posted Tuesday, May 27, 2008 9:48 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 11:35 AM
Points: 5,734, Visits: 6,917
The big question in my mind would be - what is it you see using this for? do you have a specific example in mind?

Understanding the background could be really helpful.

For what it's worth - Jeff Moden has an article on emulating multi-dimension arrays being passed in. It posted last week as I recall. Might be worth starting there.


----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Post #507032
Posted Tuesday, May 27, 2008 5:45 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 8:02 PM
Points: 16,187, Visits: 8,831
Thanks for the "plug", Matt! :)

The article Matt speaks of is at the following URL...

http://www.sqlservercentral.com/articles/T-SQL/63003/


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #507338
Posted Friday, May 30, 2008 5:55 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 9:28 AM
Points: 228, Visits: 280
Jeff/Matt,

Thanks for your help, I've decided to go down the Tally table route as it suits what I need with very little effort.

BTW Jeff, good article on Tally tables!
Post #508986
Posted Friday, May 30, 2008 9:14 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 8:02 PM
Points: 16,187, Visits: 8,831
Thanks for the feedback, David!

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #509187
Posted Sunday, June 01, 2008 7:20 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, June 30, 2009 10:36 AM
Points: 474, Visits: 163
I personal like xml for these situations.

And here is my why of implementing this

Options---> //
//
//
//
ProuductID --> //= 7


ALTER FUNCTION  [dbo].[SplitList]
(
@list AS XML
)
RETURNS TABLE
AS
RETURN
(
SELECT tempTable.item.value('.', 'VARCHAR(MAX)') AS ProductID
FROM @list.nodes('list/item') tempTable(item)
);

ALTER PROC [dbo].[uspUtbProduct_Options_Update_Ext]

@List XML,
@ProductID INT,
@UpdateFalse0InsertTrue1 BIT

AS

SET NOCOUNT ON;

DECLARE @ProductOptionIDs INT, @Cnt INT, @ProductOptionID INT;
DECLARE @TableVar table
(ID int identity(1,1) PRIMARY KEY,
ProductID INT NOT NULL
)
INSERT INTO @TableVar(ProductID)
SELECT ProductID FROM [SplitList](@List);

SELECT @Cnt = LEN(ProductID) FROM [SplitList](@List)

WHILE(@Cnt > 0)
BEGIN

SELECT @ProductOptionID = ProductID FROM @TableVar WHERE ID = (@Cnt);

IF(@UpdateFalse0InsertTrue1 = 0)
BEGIN


UPDATE Production.utbProductUtbProductOption
SET ProductOptionID = @ProductOptionID
WHERE Production.utbProductUtbProductOption.ProductID = @ProductID
END

IF(@UpdateFalse0InsertTrue1 = 1)
BEGIN
INSERT INTO [Production].[utbProductUtbProductOption]
([ProductID]
,[ProductOptionID])
VALUES
(@ProductID
,@ProductOptionID)
END

SET @Cnt = @Cnt-1
END

SET NOCOUNT OFF;

    protected void btnAddOpts_Click(object sender, EventArgs e)
{
HiddenField hdn = null;
CheckBox ckBx = null;
ArrayList prodIDs = null;
ProductsDataContext pc = null;
try
{
prodIDs = new ArrayList();
for (short i = 0; i < this.lvProdOpts.Items.Count; i++)
{
ckBx = this.lvProdOpts.Items[i].FindControl("ckBx") as CheckBox;
if (ckBx.Checked)
{
hdn = this.lvProdOpts.Items[i].FindControl("hdn") as HiddenField;
prodIDs.Add(int.Parse(hdn.Value));
}
}
if (prodIDs.Count > 0)
{
pc = new ProductsDataContext();
int res = pc.uspUtbProduct_Options_Update_Ext
(GetXml(prodIDs), int.Parse(this.ViewState["ProductID"].ToString()), true);

this.LoadListViewProductOptions();
}

//
//
//
//
} //7
catch (Exception)
{ }
hdn = null;
ckBx = null;
prodIDs = null;
pc = null;
}


 public static XElement GetXml(ArrayList list)
{
XElement x = new XElement("list");

foreach (int prodID in list)
{
x.Add(new XElement("item", prodID));
}

return x;

}



There's my side and there's your opinion !
Post #509625
« Prev Topic | Next Topic »


Permissions Expand / Collapse