Archive

Archive for the ‘SQL Server’ Category

Case Sensitive SQL Query with SQL Server

January 20th, 2010 admin No comments

ปกติแล้ว sql server ถ้าเรา select อะไรก็ตามแล้ว where name = ‘oue’ ผลลัพธ์ที่ได้มาก็จะมีทั้ง oue, Oue, oUe, ouE เพราะว่า default เป็น non Case Sensitive แต่ถ้าผมต้องการให้ query เฉพาะ ‘oue’ ล่ะจะทำอย่างไร ก็ทำตาม code ด้านล่างเลยครับ

1
2
3
select *
from user
where nickname COLLATE Latin1_General_CS_AS = 'oue'

แค่นี้เอง สั้นๆง่ายๆ ครับ

Categories: SQL Server Tags: ,

ROW_NUMBER() in sql server 2005

November 19th, 2009 admin 1 comment

ปกติเวลาที่เราต้องการ select ของโดยกำหนดไว้ว่าต้องไม่เกิน 10 row นะ ก็จะใช้

1
select top 10 * from table1

แต่ถ้าบอกว่า ต้องการ select ของโดยกำหนดว่า เอา row ที่ 10 – 20 เริ่มจะสงสัยละว่าทำอย่างไร เพราะ sql server กำหนดได้แค่ว่าไม่ให้เกินกี่ตัว แต่โชคดีที่ sql server 2005 มี ROW_NUMBER() ให้ใช้ (เพิ่งมีใน 2005) วิธีใช้ก็ตามนี้เลยครับ

1
2
3
4
select ROW_NUMBER() OVER(order by ViewId asc) as ROW_NUMBER
, ViewId
, TopicShow
from mView

sql ด้านบนนี้จะได้เลขของ row มา ทำให้เราสามารถทำ sub query เพื่อเลือกเฉพาะ row ที่เราต้องการได้

1
2
3
4
5
6
7
8
select *
from (
select ROW_NUMBER() OVER(order by ViewId asc) as ROW_NUMBER
, ViewId
, TopicShow
from mView ) as news
where ROW_NUMBER > 10
and ROW_NUMBER <= 20

เพิ่มเติมอีกเล็กน้อย เพื่อทำให้ query รวดเร็วยิ่งขึ้น จาก sql ข้างบนจะเห็นว่า sub query จะ select ของทุกอย่างใน table ก่อน แล้วค่อยให้ query ด้านนอก filter อีกครั้ง ซึ่งเปลือง process พอสมควร จึงแก้ไขด้วยการใส่ top 20 ลงไปใน sub query เพื่อลด process กันหน่อย

1
2
3
4
5
6
7
8
select *
from (
select top 20 ROW_NUMBER() OVER(order by ViewId asc) as ROW_NUMBER
, ViewId
, TopicShow
from mView ) as news
where ROW_NUMBER > 10
and ROW_NUMBER <= 20

ใครเข้ามาอ่านแล้วเห็นว่ามีประโยชน์ก็ comment หน่อยนะคับ (หาเสียงกันสุดๆ ^ ^)
ไว้พบกันใหม่กับ post ต่อไป
ถ้าไม่พบปัญหา คงไม่ได้มาเขียนบทความให้อ่านแน่ๆเลย >.<

Change Owner Name for SQL Server (Table, Stored Procedures)

October 8th, 2009 admin 1 comment

Use this command below to change the owner name of table or stored procedures in SQL Server

1
2
exec sp_changeobjectowner 'TABLE_NAME', 'dbo'
exec sp_changeobjectowner 'STORED_PROCEDURES_NAME', 'dbo'

Alter table statement in sql server

August 2nd, 2009 admin No comments

If you want to add some column in a table. You might use the sql server management tool or other tools to manage this with their’s easy UI. But if you are in some situation that haven’t the management tools to manage the table. You might use the sql script to do that. It’s call Alter Table.

Actually I found a problem with web sql management tools (with parallels Plesk panel) because when I change a column in table then the owner of table will change and the application in this server cann’t connected with this table. This’s my serious problem. -*-

Hope you enjoin to use this solution.

To add a column in table :
ALTER TABLE table_name
ADD column_name datatype

To drop a column in table :
ALTER TABLE table_name
DROP COLUMN column_name

To change a datatype of column in table :
ALTER TABLE table_name
ALTER COLUMN column_name datatype

Cross server query (Sql Server)

June 22nd, 2009 admin No comments

When using sql server query crossing 2 or more server in the same query.
try this

select * from OPENDATASOURCE(
‘SQLOLEDB’,
‘Data Source=111.111.111.11;User ID=uid;Password=pass’
).DBName.dbo.TableName

But if you query crossing 2 or more database form 1 server.
try this

select * from DBName.dbo.TableName