SQL Serverda text deger içeren bir kolonda like ile sorgulama yapmak istediğinizde arama kriteriniz sorgularda özel anlamı olan karakterler içeriyorsa [] içinde kullanabilirsiniz
Örneğin "_" içere arama kriterlerinde [] içinde yazarsanız sorgunu o kolonda içinde "_" geçen kayıtları döndürecektir...
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
ExecuteNonquery ile çalıştırdığınız sql ifadeleri etkilenen satır sayısı olarak -1 döndürüyor ise muhtemelen çalıştırdığınız stored procedure içinde aşağıdaki ifade vardır.
SET NOCOUNT ON;
Bu ifade var ise çalışıtırılan sorgunun sonucunda dönen (ya da etkilenen) satır sayısı döndürülmez. Select ifadeleri için mantıklı olan bu opsiyon silme, güncelleme ve ekleme işlemlemleri için anlamsızdır. İfadeyi kaldırın ya da OFF yapın böylece etkilenen satır sayını alabilirsiniz.
Not: Bu ifade sql server management studio tarafından yeni yaratılan stored procedurelere otomatik olarak eklenmektedir. Yazacağınız stored procedure insert, update, delete yapacak ise kaldırın.
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
All of the web applications that i am responsible to maintain have lots of stored procedures. And to many of them builds a SQL string and executes at the end of the procedure. Previous programmers wrote these SPs in that way because they don’t know how to handle comma separated ID values. For these cases a small table valued split function comes to help.
CREATE FUNCTION [dbo].[Split]
(
@delimited nvarchar(max),
@delimiter nvarchar(100)
) RETURNS @t TABLE
(
val nvarchar(max)
)
AS
BEGIN
declare @xml xml
set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>'
insert into @t(val)
select
r.value('.','varchar(5)') as item
from @xml.nodes('//root/r') as records(r)
RETURN
END
You can use this function to split array of ID with IN statement.
SELECT * FROM dbo.Split(N'a,b,c,d,e', ',')
SELECT * FROM dbo.Split('1,3,6,77,34,22', ',')
select * from Members where id in (SELECT Val as Id FROM dbo.Split('2707,2708', ','))
Happy coding…
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
It's always hard to make modifications to code or database design especially if you don't know anything about the business process.
In sql server 2005 (or 2008) you can use sp_depends stored procedure to obtain dependency between the objects such as table, view or stored procedure.
For example just run 'sp_depends Users' script to determine which views or storep porcedures are using this table.
If you use this stored procedure with views or stored procedures it can show you the objects used in it and objects uses it.
More Information: sp_depends (Transact-SQL)
Happy codding...
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
If you encounter a deadlock problem this resources can help to understand and solve the problem.
Deadlocking
http://msdn.microsoft.com/en-us/library/ms177433.aspx
Minimizing Deadlocks
http://msdn.microsoft.com/en-us/library/ms191242.aspx
Detecting and Ending Deadlocks
http://msdn.microsoft.com/en-us/library/ms178104.aspx
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
You can use T-Sql's COALESCE function to combine your data into a single string. Here is a simple sample:
DECLARE @str VARCHAR(100)
SELECT @str = COALESCE(@str + '|', '') + Firstname
FROM Members with(nolock)
Print @str
Result
Ilbay|Talha|Roberte|Theıoden|Cihan|Sagolee|Kazım|Mehmet|Recep|Alp|Haci|Ömer
Happy coding...
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
I was gettin this error when i try to execute a stored procedure from linked server.
Executed as user: XXX\yyy. The OLE DB provider "SQLNCLI"
for linked server "LSQL02" reported an error.
Authentication failed. [SQLSTATE 42000] (Error 7399) Cannot initialize the data source object of OLE DB provider "SQLNCLI" for linked server "LSQL02". [SQLSTATE 42000] (Error 7303) OLE DB provider "SQLNCLI" for linked server "LSQL02" returned message "Invalid authorization specification". [SQLSTATE 01000] (Error 7412). The step failed.
To solve you can map Servers local user to remote servers user with executing 'sp_addlinkedsrvlogin'
EXEC sp_addlinkedsrvlogin @rmtsrvname = 'linkedservername'
, @useself = 'FALSE'
, @locallogin = 'localuser'
, @rmtuser = 'remoteuser'
, @rmtpassword = 'remoteuserpassword'
To remove user mapping.
EXEC sp_droplinkedsrvlogin @rmtsrvname = 'linkedservername'
,@locallogin = 'localuser'
References
Security for Linked Servers
http://msdn.microsoft.com/en-us/library/ms175537.aspx
sp_addlinkedsrvlogin (Transact-SQL)
http://msdn.microsoft.com/en-us/library/ms189811.aspx
sp_droplinkedsrvlogin (Transact-SQL)
http://msdn.microsoft.com/en-us/library/ms186218.aspx
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
You can switch the bit field's value with single update statement by using exclusive or operator.
UPDATE AdminUsers
SET IsActive = IsActive ^ 1
WHERE Id = 42
Happy codding...
Referance: ^ (Bitwise Exclusive OR) (Transact-SQL)
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
When you are developing application you may need to store timespan database. (Assuming you are using Sql Server 2005)
TimeSpan.TotalSeconds can be used to store. This returns a double. And maps to decimal data type of the sql server. For reverse action to get the time span you can use TimeSpan.FromSeconds method. This will give you the nearest millisecond value of the stored data.
But if you need accuracy you can use TimeSpan.Ticks method is suitable for you. This returns Int64 (long) and maps to bigint data type for to store in db.You can convert it to TimeSpan with the TimeSpan.FromTicks method.
"Using Ticks more accurate then FromSeconds"
Sample (.aspx page):
1 string startTime = "13:46:33.5268767";
2 string endTime = "13:47:03.1988761";
3 DateTime startDate = Convert.ToDateTime(startTime);
4 DateTime endDate = Convert.ToDateTime(endTime);
5
6 Response.Write(string.Format("Start Date : {0}<br/>", startDate));
7 Response.Write(string.Format("End Date : {0}<br/>", endDate));
8
9
10 TimeSpan timeSpan = endDate - startDate;
11 double timeSpanValue1 = timeSpan.TotalSeconds;
12 long timeSpanValue2 = timeSpan.Ticks;
13
14 Response.Write(string.Format("From Seconds : {0} (Rounded To Nearest Millisecond)<br/>",TimeSpan.FromSeconds(timeSpanValue1)));
15 Response.Write(string.Format("From Ticks : {0} (More Accurate)<br/>", TimeSpan.FromTicks(timeSpanValue2)));
Happy coding...
1 kişi tarafından 4.0 olarak değerlendirildi
- Currently 4/5 Stars.
- 1
- 2
- 3
- 4
- 5
Sql serverda collation kolon seviyesinde ayarlanabilir. Farklı collation ayarlarına sahip tablolar üzerinde sorgu yaparken aşağıdaki hata ile karşılaşırsınız.
"Cannot resolve collation conflict for column X in SELECT statement."
Bu sorunu basitçe aşmak için sorunu yaratan kolon (hata mesajında X sıra numarası ile bildirilen kolon) için "COLLATE DATABASE_DEFAULT" kullanmaktır
[KolonAdi] COLLATE DATABASE_DEFAULT
DATABASE_DEFAULT yerine açık olarak istediğiniz collation adını da yazabilirsiniz.
Ör: SQL_Latin1_General_CP1254_CI_AS, Turkish_CI_AS
İlave: Bir tablodaki kolonun collation ayarini degistirmek icin sunu yazabilirsiniz
ALTER TABLE dbo.Product
ALTER COLUMN [Description]
varchar(max)
COLLATE Latin1_General_CI_AS
NOT NULL
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5