Thursday, September 27, 2012
sqlbulkcopy with transaction
string connstring = _hsconnstring.Substring(1, (_myconnstring.Length - 1));
using (SqlConnection destinationConnection = new SqlConnection((connstring)))
{
destinationConnection.Open();
using (SqlTransaction transaction = destinationConnection.BeginTransaction())
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection,SqlBulkCopyOptions.Default,transaction))
{
try
{
bulkCopy.DestinationTableName = "myfirsttable";
bulkCopy.WriteToServer(outTable1);
bulkCopy.DestinationTableName = "mysecondtable";
bulkCopy.WriteToServer(outTable2);
bulkCopy.DestinationTableName = "mythirdtable";
bulkCopy.WriteToServer(outTable3);
bulkCopy.DestinationTableName = "myfouthtable";
bulkCopy.WriteToServer(outTable4);
bulkCopy.DestinationTableName = "myfifthtable";
bulkCopy.WriteToServer(outTable5);
bulkCopy.Close();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
Utilities.HandleError("Error at data bulk insert: ", ex);
throw ex;
}
}
}
}
number of current open sql session using query
--SP
exec sp_who
--QUERY
SELECT
DB_NAME(dbid) as DBName,
COUNT(dbid) as NumberOfConnections,
loginame as LoginName
FROM
sys.sysprocesses
WHERE
dbid > 0
GROUP BY
dbid, loginame
order by DB_NAME(dbid)
Monday, September 24, 2012
SQL create split function and how to use function in sql
ALTER FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1))
returns @temptable TABLE (items varchar(8000))
as
begin
declare @idx int
declare @slice varchar(8000)
select @idx = 1
if len(@String)<1 br="br" is="is" nbsp="nbsp" null="null" or="or" return="return" tring="tring">
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
GO1>
--- How to call the function
select * from [dbm].[Split] ('L196,L201,L197',',')
Subscribe to:
Posts (Atom)