I would like to know if i can select more then 1 uniqueidentifier in SQL server.
To select 1 : SELECT NEWID() this brings back 1 result.
I would like to bring back like 50 results
EDIT:
I would like the results to be returned in 1 grid, so i can copy all of them at once. Not copy and paste 1 by 1.
Are you trying to do this in SQL Server Management Studio?
Try:
SELECT NEWID()
GO 50
and run this batch
Update:
OK - how about this then??
SELECT NEWID()
FROM master..spt_values
WHERE name IS NULL
AND number < 50
Assuming the master.dbo.sysobjects table has at least 50 system objects in it:
SELECT TOP 50 NEWID() FROM master.dbo.sysobjects WHERE xtype = 'S'
You don't need an order by, since the NEWID is random every time.
--run these queries independently
CREATE TABLE #temp1 (ID UniqueIdentifier)
GO
INSERT INTO #temp1
SELECT NewID() AS ID
GO 50
SELECT *
FROM #temp1
GO
DROP TABLE #temp1
GO
Related
Hello everyone I would like to ask you that how could I select previous record in sql server 2008 like image below if I stand on "ACCOUNTING MANAGER" I would like to select "SELLER"
It's a better way to put primary key in the table. So create a primary key in your table.
select top 1 t1.* from table1 t1, table1 t2
where t1.primaryKey = t2.primaryKey - 1 order by primaryKey desc
You can try this.
first of all you need to add one column as Identity.
ALTER TABLE Your_TableName ADD AUTOID INT IDENTITY(1,1)
Then you need to find the rowID of the record where you are standing right now (i.e. "ACCOUNTING MANAGER").
declare #RowID INT
Set #RowID=(Select AUTOID from Your_TableName where JOBDESC="ACCOUNTING MANAGER")
AND then
select * from Your_TableName where AUTOID=(#RowID-1)
#RowID -1 if you want previous record.
#RowID+1 if you want next record.
use cte like create rownumber() using row_number function....
with temp as (select *,row_number()over( order by [tooday] asc) as rn from tablename )
select t1.jobDESC from temp t1
join temp t2 on t1.rn =t2.rn-1
where t2.jobDESC = 'ACCOUNTING MANAGER'
Note change tablename with table name ..
need help for this, can SQL do this?
I have a table with 100 rows, I need to call 10 random rows from the table.
Can SQL make every query call give me different random data but not the chosen rows before
1st random query = get 10 random rows
2nd random query = get another 10 random rows different from the 1st call
.
.
.
10th random query = get the last 10 rows not chosen
I am still searching but no idea how, last option is every select do updates to the rows so not selected in next call
SELECT TOP 10 [your select list]
FROM [your table]
ORDER BY NEWID()
Should give you 10 "random" rows per call.
If you need the rows to be 100% sure they're not selected previously - the only way to do it is to mark them so you can exclude them in where or remove them from table.
edit to expand on the mark approach:
Pseudo code for marking as extracted could look something like this:
DECLARE #tempList AS TABLE
INSERT INTO #tempList
SELECT TOP 10 [your select list]
FROM [your table]
WHERE extracted = 0
ORDER BY NEWID()
UPDATE [your table]
SET extracted = 1
WHERE rows IN #tempList
SELECT * FROM #templist
With this script, you just need to declare which run you are doing, it will always give the same rows for each run, you just have to change the value for #run. Result will seem random without actually being random:
DECLARE #run INT = 2
DECLARE #t table(id int)
INSERT #t
SELECT x.id*10 + y.id + 1
FROM (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) x(id)
CROSS JOIN
(values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) y(id)
;WITH CTE as
(
SELECT id, ntile(10) over (order by reverse(CHECKSUM(id, 'abc'))) rowgroup FROM #t
)
SELECT id
FROM CTE
WHERE rowgroup = #run
I want to select list of records using Id. I am having order like 254,265,10,258 like that. I want to select the same order. But when i write a query in sql server it is automatically take the order 10,254,258,265 like that.
How to rectify this using query in sql server 2008?
I am having 1000 records like that. Please help me any one?
if you want specific order like 254,265,10,258, use a temp table or table varialbe
declare #IDs table
(
seq int identity(1,1),
ID int
)
insert into #IDs (ID) select 254
insert into #IDs (ID) select 265
insert into #IDs (ID) select 10
insert into #IDs (ID) select 258
SELECT *
FROM #IDs i INNER JOIN yourtable t ON i.DI = t.ID
ORDER BY i.seq
another way is specify the ordering in ORDER BY clause
ORDER BY (CASE ID when 254 then 1 when 265 then 2 when 10 then 3 when 258 then 4 end)
No matter what, you will still need a ORDER BY clause
I have a very meaty stored procedure in a SQL Server 2000 DB which returns a single resultset. I don't want to (not allowed to) touch the original SP but would like add pagination to the returned records.
Is it possible to wrap this SP with another that takes the returned resultset and only gives me rows X to Y ?
create procedure ProcWrap
as
declare #T table (ID int, Name nvarchar(50))
insert into #T
exec ProcToWrap
select *
from #T
where ID < 10
Edit 1
Don't have SQL Server 2000 to test on and I don't remember if table variables where available then. Here is a procedure using a temp table instead. Added a RowNum identity column that you can use for pagination.
create procedure ProcWrap2
as
create table #T (RowNum int identity, ID int, Name nvarchar(50))
insert into #T
exec ProcToWrap
select *
from #T
where RowNum between 10 and 19
drop table #T
Edit 2
Output from ProcToWrap in this case is columns ID and Name. RowNum is generated automatically.
Get the results from the SP and put them in a temporary table, then you can select X results from that table.
As others have said you will have to put the results of the procedure in a temp table then select the rows you want from that.
To get a set of rows from your results you need to use the ROW_NUMER() function:
SELECT
ROW_NUMBER() OVER (ORDER BY ID) AS row_number, *
FROM
Your_Temp_Table
WHERE row_number BETWEEN 11 AND 20 -- For the second page of results with 10 per page.
EDIT: Just realised you are using SQL Server 2000 which does not have ROW_NUMBER(), sorry
EDIT2: Since you are storing the results of the query in a temp table you can add an incrementing integer field to that result set and use that as a simulation for the ROW_NUMBER() in order to select the row you need.
EDIT3: Here's a link to an article discussing pagination in SQL Server 2000
Writing my first SQL query to run specifically as a SQL Job and I'm a little out of my depth. I have a table within a SQL Server 2005 Database which is populated each day with data from various buildings. To monitor the system better, I am attempting to write a SQL Job that will run a query (or stored procedure) to verify the following:
- At least one row of data appears each day per building
My question has two main parts;
How can I verify that data exists for each building? While there is a "building" column, I'm not sure how to verify each one. I need the query/sp to fail unless all locations have reported it. Do I need to create a control table for the query/sp to compare against? (as the number of building reporting in can change)
How do I make this query fail so that the SQL Job fails? Do I need to wrap it in some sort of error handling code?
Table:
Employee RawDate Building
Bob 2010-07-22 06:04:00.000 2
Sally 2010-07-22 01:00:00.000 9
Jane 2010-07-22 06:04:00.000 12
Alex 2010-07-22 05:54:00.000 EA
Vince 2010-07-22 07:59:00.000 30
Note that the building column has at least one non-numeric value. The range of buildings that report in changes over time, so I would prefer to avoid hard-coding of building values (a table that I can update would be fine).
Should I use a cursor or dynamic SQL to run a looping SELECT statement that checks for each building based on a control table listing each currently active building?
Any help would be appreciated.
Edit: spelling
You could create a stored procedure that checks for missing entries. The procedure could call raiserror to make the job fail. For example:
if OBJECT_ID('CheckBuildingEntries') is null
exec ('create procedure CheckBuildingEntries as select 1')
go
alter procedure CheckBuildingEntries(
#check_date datetime)
as
declare #missing_buildings int
select #missing_buildings = COUNT(*)
from Buildings as b
left join
YourTable as yt
on yt.Building = b.name
and dateadd(dd,0, datediff(dd,0,yt.RawDate)) =
dateadd(dd,0, datediff(dd,0,#check_date))
where yt.Building is null
if #missing_buildings > 0
begin
raiserror('OMG!', 16, 0)
end
go
An example scheduled job running at 4AM to check yesterday's entries:
declare #yesterday datetime
set #yesterday = dateadd(day, -1, GETDATE())
exec CheckBuildingEntries #yesterday
If an entry was missing, the job would fail. You could set it up to send you an email.
Test tables:
create table Buildings (id int identity, name varchar(50))
create table YourTable (Employee varchar(50), RawDate datetime,
Building varchar(50))
insert into Buildings (name)
select '2'
union all select '9'
union all select '12'
union all select 'EA'
union all select '30'
insert into YourTable (Employee, RawDate, Building)
select 'Bob', '2010-07-22 06:04:00.000', '2'
union all select 'Sally', '2010-07-22 01:00:00.000', '9'
union all select 'Jane', '2010-07-22 06:04:00.000', '12'
union all select 'Alex', '2010-07-22 05:54:00.000', 'EA'
union all select 'Vince', '2010-07-22 07:59:00.000', '30'
Recommendations:
Do use a control table for the buildings - you may find that one
already exists, if you use the Object
Explorer in SQL Server Management
Studio
Don't use a cursor or dynamic SQL to run a loop - use set based
commands instead, possibly something
like the following:
SELECT BCT.Building, COUNT(YDT.Building) Build
FROM dbo.BuildingControlTable BCT
LEFT JOIN dbo.YourDataTable YDT
ON BCT.Building = YDT.Building AND
CAST(FLOOR( CAST( GETDATE() AS FLOAT ) - 1 ) AS DATETIME ) =
CAST(FLOOR( CAST( YDT.RawDate AS FLOAT ) ) AS DATETIME )
GROUP BY BCT.Building