Thursday, August 21, 2008

Execute sql query with exec or execute and Passing Comma Seprated List of IDs to Store Procedure

Hi,

I was in need to create the query in string variable and execute the same.I found the solution which fits in my situation.

The below mentioned way gives you idea where i need that.As i have the list of values and i want only those records to be retrive from the Database.

EXECUTE(@STRQRY)

ALTER procedure [dbo].[spGetData]
@strDataIDs varchar(max)
as
begin
DECLARE @STRQRY NVARCHAR(MAX)
SET @STRQRY= 'select * from DataMaster where accnum not in (' +@strDataIDs+ ')'
EXECUTE(@STRQRY)
end

Passing Array to Store Procedure in SQL

Passing the array as SQL Parameter is always a concern becuase running the ExceuteQuery for multiple Insert is not a good option through .NET gives you the Implementation of Transaction but this will only serve concurrency issue but somewhere er have to face the performance issue because of this.

So, the need to pass the array to Store Procedure is very comman and the best approch to do this is pass the xml to the Store Procedure in ntext variable.


Create procedure [dbo].[spSendXML]
(
@RecordXML ntext
)
AS

Begin

create Table #tbl (ID int)

DECLARE @idoc int
EXEC sp_xml_preparedocument @idoc OUTPUT, @RecordXML

insert into #tbl
select ID
FROM OPENXML(@idoc,'/Root/Comp', 1)
WITH (ID varchar(50))


End


By doing this you can get all the data in SQL Temp Table.


Calling of the same Store Procedure.

spSendXML ''

Thanks
Dalip Vohra

Custom Text Box with ID and Text in read only mode.

I was having the situation where I have to show the account name in the application and consider accountid in functioning like insert, update.

Point to note here is i don't want to se any kind of list control like dropdown,listbox,etc.

So, I have created my own Text Box control which will provide me the facility to maintain the ID of the text.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public class MyTextBox : TextBox
{
private string _valueID;
public string ValueID
{
get
{
return _valueID;
}
set
{
_valueID = value;
}
}
protected override void OnCreateControl()
{
this.ReadOnly = true;
base.OnCreateControl();
}
}
}

Thanks
Dalip Vohra

Fetching Values Of Sql Table as Comma Seprated Values In SQL Variable

Declare @Str varchar(2000)

Begin

(Select @Str=COALESCE(@Str, '','') + Cast([Employee_name] as nvarchar(100)) + ','

from dbo.Employee )

Select substring(@Str,0,len(@str))

End