Contents

1 ろ 一危 襷り鍵
2 NewID() 襯 伎 覦覯
3 TableSample 伎 覦覯
4 RAND() 伎蠍


1 ろ 一危 襷り鍵 #

CREATE TABLE users
(
uid int,
uname varchar(30)
)
GO

--Inserting 15 rows into the table users
SET NOCOUNT ON
INSERT [users] ([uid],[uname])VALUES(1,'Vyas Kondreddi')
INSERT [users] ([uid],[uname])VALUES(2,'Narayana Kondreddi')
INSERT [users] ([uid],[uname])VALUES(3,'Kondreddi')
INSERT [users] ([uid],[uname])VALUES(4,'Narayana Vyas Kondreddi')
INSERT [users] ([uid],[uname])VALUES(5,'Subhadra Jyothsna')
INSERT [users] ([uid],[uname])VALUES(6,'Geetha Madhuri Vungarala')
INSERT [users] ([uid],[uname])VALUES(7,'Sridhar Vungarala')
INSERT [users] ([uid],[uname])VALUES(8,'Vamsee Mohan')
INSERT [users] ([uid],[uname])VALUES(9,'Gopala Krishna Kondreddi')
INSERT [users] ([uid],[uname])VALUES(10,'Nagaja Devi Kondreddi')
INSERT [users] ([uid],[uname])VALUES(11,'Vijaya Madhuri Govindaraju')
INSERT [users] ([uid],[uname])VALUES(12,'Pamarthy Pradeep Kumar')
INSERT [users] ([uid],[uname])VALUES(13,'Pamarthy Harinath')
INSERT [users] ([uid],[uname])VALUES(14,'Vungarala Sathyanarayana')
INSERT [users] ([uid],[uname])VALUES(15,'Pamarthy Ramakrishna')
SET NOCOUNT OFF
GO

2 NewID() 襯 伎 覦覯 #

SELECT TOP 1
	uid
,	uname
FROM users
ORDER BY NewID()

一危一 襷朱 焔 .

3 TableSample 伎 覦覯 #

WHILE(1=1)
BEGIN
	SELECT TOP 1
		uid
	,	uname
	FROM users TABLESAMPLE(200 ROWS)
	
	IF @@ROWCOUNT > 0
		BREAK
END
一危郁 譬 襷 襦 襾轟 . 'TABLESAMPLE(n ROWS)' n 2 ~ 3 Page れ願 row覃 蟆企.

4 RAND() 伎蠍 #

--朱朱..
DECLARE @mx int, @mn int
SELECT @mx = MAX(uid), @mn = MIN(uid) FROM users
SELECT uid,uname [Lucky Winner is...] FROM users WHERE uid = ROUND(@mn + (RAND() * (@mx-@mn)),0)


--襷 譴螳 企宏 觜語 1螳 Row 覦讌 覈詩 企...

DECLARE @mx int, @mn int, @uid int, @uname varchar(200)
SELECT @mx = MAX(uid), @mn = MIN(uid) FROM users
WHILE(1=1)
BEGIN
	SELECT 
		@uid = uid
	,	@uname = uname 
	FROM users WHERE uid = ROUND(@mn + (RAND() * (@mx-@mn)),0)
	IF @@ROWCOUNT > 0 
		BREAK
END

SELECT @uid, @uname
覓企 一危郁 朱 願鍵 蟾讌 覦覯 伎螻, 一危郁 襷 企 Tablesample 伎 蟆 譬蟆.