select @[email protected]('*')
for xml raw,type
Above statement will generate following alert:
Msg 6819, Level 16, State 3, Line 2
The FOR XML clause is not allowed in a ASSIGNMENT statement.
I have a table with 600+ columns imported from a csv with special chars % _ - in the column names, is there a way to change the column names to remove these special chars ?
the code can be tsql or tsql
Hi
I am wondering how can do a mass insert and bulk copy at the same time? I have 2 tables that should be affect by the bulk copy as they both depend on each other.
So I want it that if while inserting table 1 a record dies it gets rolled back and table 2 never gets updated. Also if table 1 inserts good and table 2 an update fails table 1 gets rolled back.
Can this be done with bulk copy?
I have the below
Name Date
A 2011-01-01 01:00:00.000
A 2011-02-01 02:00:00.000
A 2011-03-01 03:00:00.000
B 2011-04-01 04:00:00.000
A 2011-05-01 07:00:00.000
The desired output being
Name StartDate EndDate
-------------------------------------------------------------------
A 2011-01-01 01:00:00.000 2011-04-01 04:00:00.000
B 2011-04-01 04:00:00.000 2011-05-01 07:00:00.000
A 2011-05-01 07:00:00.000 NULL
How to achieve the same using TSQL in Set based approach
DDL is as under
DECLARE @t TABLE(PersonName VARCHAR(32), [Date] DATETIME)
INSERT INTO @t VALUES('A', '2011-01-01 01:00:00')
INSERT INTO @t VALUES('A', '2011-01-02 02:00:00')
INSERT INTO @t VALUES('A', '2011-01-03 03:00:00')
INSERT INTO @t VALUES('B', '2011-01-04 04:00:00')
INSERT INTO @t VALUES('A', '2011-01-05 07:00:00')
Select * from @t
I would like to create a stored procedure that takes in a string of comma separated values like this "1,2,3,4", and break it apart and use those numbers to run a query on a different table.
so in the same stored procedure it would do something like
select somefield from sometable where somefield = 1
select somefield from sometable where somefield = 2
select somefield from sometable where somefield = 3
select somefield from sometable where somefield = 4
Thanks!
I'm trying to write a stored procedure to select employees who have birthdays that are upcoming.
SELECT * FROM Employees WHERE Birthday > @Today AND Birthday < @Today + @NumDays
This will not work because the birth year is part of Birthday, so if my birthday was '09-18-1983' that will not fall between '09-18-2008' and '09-25-2008'.
Is there a way to ignore the year portion of date fields and just compare month/days?
This will be run every monday morning to alert managers of birthdays upcoming, so it possibly will span new years.
Here is the working solution that I ended up creating, thanks Kogus.
SELECT * FROM Employees
WHERE Cast(DATEDIFF(dd, birthdt, getDate()) / 365.25 as int)
- Cast(DATEDIFF(dd, birthdt, futureDate) / 365.25 as int)
<> 0
I have a simple problem, I think, but I have googled and can't find the solution. I have a cube that has MeasureA, MeasureB and MeasureC. Not all three measures have values for each record, sometimes they can be null, it's depending if it was applicable.
Now for my totals, I need to average but the average must not take nulls into account. Any help will be much appreciated. When I view the measures, the null values show as zeros.
for eg...
SELECT *
FROM ( SELECT RANK() OVER (ORDER BY stud_mark DESC) AS ranking,
stud_id,
stud_name,
stud_mark
FROM tbl_student ) AS foo
WHERE ranking = 10
Here foo is present...actually what it does ?..
I've banging my head for hours, it seems simple enough, but here goes:
I'd like to create a view using multiple select statements that outputs a Single record-set
Example:
CREATE VIEW dbo.TestDB
AS
SELECT X AS 'First'
FROM The_Table
WHERE The_Value = 'y'
SELECT X AS 'Second'
FROM The_Table
WHERE The_Value = 'z'
i wanted to output the following recordset:
Column_1 | Column_2
'First' 'Second'
any help would be greatly appreciated!
-Thanks.
When i query
INFORMATION_SCHEMA.VIEWS it list all views but when i query
INFORMATION_SCHEMA.VIEW_TABLE_USAGE it displays only few views.
How can i rebuild all the views info in INFORMATION_SCHEMA.VIEW_TABLE_USAGE?
I have a table like this
foo(id, parentId)
-- there is a FK constraint from parentId to id
and I need to delete an item with all his children and children of the children etc.
anybody knows how ?
I have a database project that goes through iterations (only one so far) and I need to deploy a testing version to a live server. I'm not sure how to go about this.
I can make all the changes in a copy and then remake those changes in the live version. That doesn't make sense.
Is there a way to change a server name to an existing server? What's the best practice for this scenario?
I have written the below SP for Precheck for Duplicate records before insert into Table .
but it is not allow me yo write insert staement inside the CASE .
how can I write Stored Procedure for fist Check the value @Ordername into table After that if it is not present then it should inserted into Database .
CREATE PROCEDURE [Test Procedure ]
(
@section varchar(70),
@mark varchar(70),
@qty decimal(18,2),
@Weight decimal(18,2),
@dateupdateremark int,
@OrderName varchar(70)
)
AS
BEGIN
SET NOCOUNT ON;
select case(@OrderName)
when (select OrderName from dbo.tbl_insertxmldetails
where(@OrderName) not in (select OrderName from tbl_insertxmldetails))
then
insert into dbo.tbl_insertxmldetails
(Section, Mark, QTY,Weight,Dateupdateremark ,OrderName,SystemDate)
values
(@Section, @Mark, @QTY,@Weight, @Dateupdateremark,@OrderName,GETDATE())
else 'File already Exists'
end
I've got a record set that consists of a start and end time in two separate fields:
id - Int
startTime - DateTime
endTime - DateTime
I'd like to find a way to query a record and return it as X records based on the number of 15 minute intervals found between the start and end times.
For example, let's say I have a record like this:
id, StartTime, EndTime
1, 1/1/2010 8:28 AM, 1/1/2010 8:47 AM
I would return 3 records, the first would represent the 8:15 interval, #2 for the 8:30 interval and then a 3rd for the 8:45 interval.
I realize this could be done using logic in an sproc, but we are trying to remain db neutral as we support multiple database engines.
At work we have a number of databases that we need to do the same operations on. I would like to write 1 SP that would loop over operations and set the database at the beginning of the loop (example to follow). I've tried sp_executesql('USE ' + @db_id) but that only sets the DB for the scope of that stored procedure. I don't really want to loop with hard coded database names because we need to do similar things in many different places and it's tough to remember where things need to change if we add another DB.
Any thoughts
Example:
DECLARE zdb_loop CURSOR FAST_FORWARD FOR
SELECT distinct db_id from DBS order by db_id
OPEN zdb_loop
FETCH NEXT FROM zdb_loop INTO @db_id
WHILE @@FETCH_STATUS = 0
BEGIN
USE @db_id
--Do stuff against 3 or 4 different DBs
FETCH NEXT FROM zdb_loop INTO @db_id
END
CLOSE zdb_loop
DEALLOCATE zdb_loop
I need to find the amount of updated rows
UPDATE Table SET value=2 WHERE value2=1
declare @aaa int
set @aaa = @@ROWCOUNT
It doesn't work. How can I do that?
I have to find the indentid from the status table based on below two conditions:
1. If there are more than one record having same indentid in status table and the same indentID has count1 in feasibilitystatus table then
I don't want to display the record.
2. If there is only one record of indentid in status table and the same indentID has count0 in feasibilitystatus table then
I don't want to display the record.
Query:
select distinct s.indentid
from status s where
s.status='true'
and s.indentid
not in(select case when count(s.indentid)>1 then (select indentid from feasibilitystatus
group by indentid having count(indentid)>1)
else (select indentid from feasibilitystatus group by
indentid having count(indentid)>0)
end as indentid from status)
Error:
An aggregate may not appear in the WHERE clause unless it is in a subquery
contained in a HAVING clause or a select list, and the column being aggregated is
an outer reference.
I have a select statement, retrieve about 1000 record
I want to modify it to return only some records defined by @startIndex and @count
e.g. : If I said @startIndex=20 and @count=20
the result will be : from the 21th record to 40th
I try to make it, but it take the same time as if I retrieve the 1000 record
what is the best way to do that
I have a table named 'Logs' with the following values :
CheckDate CheckType CheckTime
-------------------------------------------
2011-11-25 IN 14:40:00
2011-11-25 OUT 14:45:00
2011-11-25 IN 14:50:00
2011-11-25 OUT 14:55:00
2011-11-25 IN 15:00:00
2011-11-25 OUT 15:05:00
2011-11-25 IN 15:15:00
2011-11-25 OUT 15:20:00
2011-11-25 IN 15:25:00
2011-11-25 OUT 15:30:00
2011-11-25 OUT 15:40:00
2011-11-25 IN 15:45:00
I want to use the previous table to produce a result of:
CheckDate CheckIn CheckOut
-----------------------------------------
2011-11-25 14:40:00 14:45:00
2011-11-25 14:50:00 14:55:00
2011-11-25 15:00:00 15:05:00
2011-11-25 15:15:00 15:20:00
2011-11-25 15:25:00 15:30:00
2011-11-25 NULL 15:40:00
2011-11-25 15:45:00 NULL
So far I have come up with this result set :
CheckDate CheckIn CheckOut
-----------------------------------------
2011-11-25 14:40:00 14:45:00
2011-11-25 14:50:00 14:55:00
2011-11-25 15:00:00 15:05:00
2011-11-25 15:15:00 15:20:00
2011-11-25 15:25:00 15:30:00
2011-11-25 15:45:00 NULL
The problem is I cannot generate the log without CheckIns :
CheckDate CheckIn CheckOut
-----------------------------------------
2011-11-25 NULL 15:40:00
The sequence of CheckIn - CheckOut pairing and order is in increasing time value.
My code does not update the thread field. It is null. Anyone have any ideas?
INSERT INTO [Messages]([Sender], [Receiver], [Job_Number], [Subject], [MessageText], [DateSent])
VALUES(@Sender, @Receiver, @Job_Number, @Subject, @MessageText, @DateSent)
SET @ThreadID = SCOPE_IDENTITY()
UPDATE [Messages]
SET Thread = @ThreadID
WHERE MessageID = @ThreadID
I make a query that return to me the count of Stored Procedure that created everyday as follow
SELECT convert(varchar, crdate, 103) as Date,Count(*) as Counter
FROM sysobjects
WHERE (xtype = 'p') AND (name NOT LIKE 'dt%')
Group by convert(varchar, crdate, 103)
and its already work but dates appear in string format that i can't order it such as below
01/03/2010 3
01/04/2008 4
01/05/2010 5
01/11/2008 1
01/12/2008 4
02/03/2008 1
02/03/2010 2
02/04/2008 4
02/05/2010 2
02/11/2008 2
02/11/2009 2
02/12/2008 4
03/01/2010 1
03/02/2010 2
03/03/2010 2
03/04/2008 2
03/04/2010 2
03/05/2008 1
03/05/2010 2
I want to make that in which date is in datetime format that i can make order by successfully, i tried convert(datetime, crdate, 103) but it show Full date
any idea of how to do ?
I have a following query. It uses only one table (Customers) from Northwind database.
I completely have no idea how does it work, and what its intention is. I hope there is a lot of DBAs here so I ask for explanation. particularly don't know what the OVER and PARTITION does here.
WITH NumberedWomen AS
(
SELECT CustomerId ,ROW_NUMBER() OVER
(
PARTITION BY c.Country
ORDER BY LEN(c.CompanyName) ASC
)
women
FROM Customers c
)
SELECT * FROM NumberedWomen WHERE women > 3
If you needed the db schema, it is here
Why does this fail:
DECLARE @DATE VARCHAR(50) = 'dasf'
SELECT CASE WHEN ISDATE(@DATE) = 1 THEN CONVERT(date,@DATE) ELSE @DATE END
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
Why is it trying to convert dasf to date when it clearly causes ISDATE(@DATE) = 1 to evaluate to false...
If I do:
SELECT ISDATE(@DATE)
The return value is 0.