|
Thanks.
I will have to check this with regards to the data I have been looking at - however I think you have hit the proverbial nail on the head.
I think I will go with changing the dates to something other than 01/01/1900 as I can see someone in the future doing an isnull comparison on an empty string and getting a match against these dates.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
GuyThiebaut wrote: as I can see someone in the future doing an isnull comparison on an
empty string and getting a match against these dates.
I don't understand that statement so just to be sure I will state that an empty string is not the same as a null value.
|
|
|
|
|
hi to all
i have a while loop like this
declare @Counter int
while(@Counter<1000)
begin
set @Counter = @Counter + 1
select * from mytable where [my where clause]
end
my where cluase change in each repeat.
i want to union all result to a table and return one table as query result
how can i do this ?
performance is very important for me
because while loop can be run for upper 15000
thanks for any help
|
|
|
|
|
mehdi.sabet wrote: my where cluase change in each repeat.
Can you post your where clause? It can probably be replaced with a single query, which will give you much better performance.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
 hello
thanks for reply
my main code is:
ALTER PROCEDURE [dbo].[lfd__GetFullPersonnelDetails]
(
@PersonnelList nvarchar(MAX),
@WorkPeriodID int,
@WorkPeriodYear int
) AS
Declare @startDate SMALLDATETIME
Declare @endDate SMALLDATETIME
Declare @PersonnelBaseID INT
WHILE(LEN(@PersonnelList) > 0)
BEGIN
SET @PersonnelBaseID = SUBSTRING(@PersonnelList, 0, PATINDEX('%,%',@PersonnelList))
IF(@PersonnelBaseID != 0)
SET @PersonnelList = SUBSTRING(@PersonnelList, LEN(CAST (@PersonnelBaseID AS NVARCHAR) + ',') + 1, LEN(@PersonnelList))
ELSE
BEGIN
SET @PersonnelBaseID=CAST(@PersonnelList AS INT)
SET @PersonnelList = SUBSTRING(@PersonnelList, LEN(@PersonnelList + ',') + 1, LEN(@PersonnelList))
END
set @startDate=(select top 1 [date] as startdate
from
lfd_DailyStatistics
where
PersonnelBaseID = @PersonnelBaseID and [Year] = @WorkPeriodYear and WorkingPeriodID = @WorkPeriodID
order by date asc)
set @endDate=(select top 1 [date] as startdate
from
lfd_DailyStatistics
where
PersonnelBaseID = @PersonnelBaseID and [Year] = @WorkPeriodYear and WorkingPeriodID = @WorkPeriodID
order by date desc)
SELECT
dailyAbsences.*,
CAST(CASE WHEN calculationQueue.[StartDate] IS NULL THEN 0 ELSE 1 END AS bit) AS [NeedsRecalculation]
FROM
lfd_dailyAbsence dailyAbsences LEFT JOIN
lfd_CalculationQueue calculationQueue ON dailyAbsences.[PersonnelBaseID] = calculationQueue.[PersonnelBaseID] AND calculationQueue.[StartDate] <= @endDate
WHERE
dailyAbsences.[PersonnelBaseID] = @PersonnelBaseID AND
dailyAbsences.[StartDate] >= @startDate AND
dailyAbsences.[EndDate] <= DATEADD(day, 1, @endDate)
ORDER BY
dailyAbsences.[StartDate]
END
start date and end date change in each repeat
|
|
|
|
|
Little tip - get rid of the order by clause - as it is taking up unnecessary processing time.
You can order the results once you have combined them.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
OK, the first thing you need is a split function:
CREATE FUNCTION dbo.fn_Split
(
@Value varchar(max),
@Delimiter varchar(20) = ','
)
Returns TABLE
As Return
(
WITH cte (Idx, StartIndex, EndIndex) As
(
SELECT
1,
1,
CharIndex(@Delimiter, @Value)
UNION ALL
SELECT
1 + Idx,
CAST(1 + EndIndex As int),
CharIndex(@Delimiter, @Value, 1 + EndIndex)
FROM
cte
WHERE
EndIndex > 0
)
SELECT
Idx,
SubString(@Value, StartIndex, CASE
WHEN EndIndex > 0 THEN EndIndex - StartIndex
ELSE LEN(@Value)
END) As Value
FROM
cte
);
GO
Then you can build a single query:
WITH ctePersonnelList (PersonnelBaseID) As
(
SELECT
CASE
WHEN Value Like '%[^0-9]%' THEN Null
ELSE CAST(Value As int)
END
FROM
dbo.fn_Split(@PersonnelList, ',')
WHERE
Value Not Like '%[^0-9]%'
),
cteDates (PersonnelBaseID, StartDate, EndDate) As
(
SELECT
L.PersonnelBaseID,
Min([date]),
Max([date])
FROM
ctePersonnelList As L
INNER JOIN lfd_DailyStatistics As S
ON L.PersonnelBaseID = S.PersonnelBaseID
WHERE
[Year] = @WorkPeriodYear
And
WorkingPeriodID = @WorkPeriodID
GROUP BY
L.PersonnelBaseID
)
SELECT
A.*,
CAST(CASE WHEN Q.StartDate IS NULL THEN 0 ELSE 1 END AS bit) As NeedsRecalculation
FROM
cteDates As D
INNER JOIN lfd_dailyAbsence As A
ON A.PersonnelBaseID = D.PersonnelBaseID
And A.StartDate >= D.StartDate
And A.EndDate <= DateAdd(day, 1, D.EndDate)
LEFT JOIN lfd_CalculationQueue As Q
ON A.PersonnelBaseID = Q.PersonnelBaseID
And Q.StartDate <= D.EndDate
ORDER BY
A.PersonnelBaseID,
A.StartDate
;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
thanks Richard
this is a good solution
|
|
|
|
|
Why not create a temp table and insert the results there? When the while loop is done select * from your temp table.
|
|
|
|
|
ok but what about performance ?
if i done this and this loop execute ,for example , for 15000 repeat
this is a optimal way?
and how can use one temp table and insert all resualt to this temp table?
thank you
|
|
|
|
|
Looping in a database is never efficient. Working with tables is.
Did that answer your question?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi
I have just changed the db for a small app from using an MS ACCESS db (terrible thing!) to SSE 2008 R2 DB. Previously, they used to just copy the ACCESS db file each evening to a flash drive, and keep that off site. They have been using the app for a year now, and the SSE backup file is only about 20MB - in other words not that much data to backup. The server instance only has one database, and one additional user, no views or stored procedures or anything like that
1) What exactly do I need to back up? The database, system databases (master, model, msdb etc) log files?
2) How should I do the backup? (Been thinking along the lines of a backup command that can be run from the app (SMO), or possibly having a table in the DB with last backup date/time, and if a user logs on, and the last backup is older that a day, it makes a new backup. Or is there a better way? Can you schedule a job on SSE to do this daily?
3) I'm also not sure what options to use with respect to:
a) Backup Type (Full or Incremental?)
b) Copy-only Backup (No?)
c) Backup to existing medias set (yes) - Overwrite or Append?
4) The code I've tested using SMO
Backup bkp = new Backup();
bkp.Action = BackupActionType.Database;
bkp.Database = databaseName;
bkp.Devices.AddDevice(fileName, DeviceType.File);
bkp.Incremental = chkIncremental.Checked;
bkp.SqlBackup(srv);
Basically I want to end up with a backup (that can possibly be copied to some or other storage device as well for off site storage - flashdisk or portable drive) that in that case of the sever crashing completely, that I could restore to at worst a new instance of SSE?
Am i on the right track here?
Any other suggestions welcome!
modified 1-May-13 2:23am.
|
|
|
|
|
Without going into details of which backup strategy you should chose - there are lots of articles out there that can do a much better job than I can - do make sure that you test your backups.
I do a weekly restore to check that the backups are working correctly - I even went as far as to deliberately corrupt the database(not an easy thing to do I can assure you) to check that a restore worked ok.
Remember - you backups are only as good as the state they return your system to should you experience a catastrophic hardware failure.
I would also recommend taking an image of your server every month - as in the case of a failure it might be easier to restore an image then backups on top of that rather that rebuild a Windows server from scratch...
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
Hi Guy
Thanks for your reply. I'm not working for the company, I only developed an app for their stock control. They don't have IT staff on site, and I'm really not sure what sort of backups they do on the servers. (They have two boxes both running Small Business Server - and I installed and instance of SSE for my app on one of them) All I want is to be able to restore the data for my application. Even if that means I install a new instance of SSE on another machine, and restore the data that was backed up.
When you say you restore a backup weekly to test, where do restore the backup to? Have you got a duplicate database that you use to test the restore? What sort of things do you check to see that restore was successful - If it restores without Errors, check row counts on the tables?
|
|
|
|
|
I restore the backup to a test system(so yes a duplicate database) then I test a series of .Net apps on the restored data as a sort of sniff test.
SQL Server is 'generally' good at telling you if a backup restored with corruption - you could run a select query on the major tables as a sniff test, just to ensure that what comes back looks ok.
As a DBA you want to make sure that when the SHTF you can get the systems back to working within an agreed SLA(which is another point - make sure that you have an agreed SLA on how long a restore of a broken system will take and double the time you think it will take you, I can't stress how important this is because when things go wrong people behave irrationally and you need to be able to hold up that piece of paper to give yourself some space, in order to get on with fixing the systems).
A few useful sites worth reading.
Backups[^]
Validating a backup[^](if you are not subscribed it is well worth joining this site)
Database integrity[^]
I would strongly recommend an image backup every month with daily incremental image backups - disk space is cheap and rebuilding systems is expensive.
Another tip - if you are not running this all on a virtual machine, ensure that the SQL Server install is on a different disk to the software accessing it(this will further ensure its integrity and speed up access).
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
GuyThiebaut wrote: I restore the backup to a test system(so yes a duplicate database) then I test a series of .Net apps
This should be easy enough to implement... I think.... (Well at least a duplicate database on the same server instance)
GuyThiebaut wrote: As a DBA you want to make sure that when the SHTF you can get the systems back to working within an agreed SLA
Good point - the guy is a mate, but he should know what is a reasonable recovery time...
GuyThiebaut wrote: I would strongly recommend an image backup every month with daily incremental image backups - disk space is cheap and rebuilding systems is expensive.
I will suggest that he implements this (I think he brings in a consultant to help out with servers etc)
GuyThiebaut wrote: Another tip - if you are not running this all on a virtual machine, ensure that the SQL Server install is on a different disk to the software accessing it(this will further ensure its integrity and speed up access).
Not entirely sure what you mean here? The app is a standalone windows forms app, installed on the workstations, so I guess that should be fine.
Thanks for the links - will have a look shortly
|
|
|
|
|
Richard.Berry100 wrote: the guy is a mate,
Even more important to have an agreed SLA as it is good to be clear on where the boundaries of being a mate and being an employer lie
Richard.Berry100 wrote: Not entirely sure what you mean here?
I take it there there is one central instance of an SQL Server rather than an instance on each workstation.
If there is one central instance then try to get it off the OS drive(probably the C: drive) so that disk access is not competing between OS calls and SQL Server calls - does that make sense?
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
GuyThiebaut wrote: I take it there there is one central instance of an SQL Server rather than an instance on each workstation.
If there is one central instance then try to get it off the OS drive(probably the C: drive) so that disk access is not competing between OS calls and SQL Server calls - does that make sense?
Yes - one server instance on the server. Any yes I see what you mean. It makes sense, and I do remember reading something like that, but when I installed, I seemed to have missed where I could specify a different install location, so it installed on the C: drive (default).. At the moment there are way more serious performance hits in the app itself (populating too many datagrid rows at a time etc) - Only realized this when I got a copy of their data a year after the initial installation... I'd been testing with a bare bones DB on my machine - so yeah what those poor users have been going though - aw shame!
|
|
|
|
|
Hi,
I have been asked solution for this.
I have Vendors where i get different excel file (diffrenct structure) from different vendor.
Vendor1 excel file
------------------
Name, Address, Age
Vendor2 Excel file
------------------
CandidateName, PhysicalAddress, Age
what ever Header Names but meaning is same.
Order of Headers are may vary different...
but i need to map in Sqlserver with correct mapping means i have table like (CandidateName,CandidateAddress,Age).... Here i need to store correct data columns which are mapped from above two excels.
Is there any solution in Sqlserver or ssis or .NET solution to identify Generic Columns of excel(xls) and map to Sqlserver Table correctly.
Thanks,
Ravikiran
|
|
|
|
|
Ravikiran72p wrote: but i need to map in Sqlserver with correct mapping means i have table like (CandidateName,CandidateAddress,Age).... Here i need to store correct data columns which are mapped from above two excels. Unless you have "something" that you can use to identify the type of data in that column, there's nothing to map. If both names and order change, then the computer will no longer be able to distinguish between a first-name (string) and a last-name (string).
I'd suggest to use the table-headers to "propose" a mapping for that particular vendor, based on the current file and your best guess (based on a "contains"-call on the header to try and identify it) and to save/use that.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Oh the joys of dealing with people who use Excel as a data source .
The only reliable way will be to maintain a mapping table for each source, Load your data into staging tables (I use bulkcopy, creating a new table based on the source), then test the structure against your map, process or spit the dummy based on the structure check.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Ravikiran72p wrote: Is there any solution in Sqlserver or ssis or .NET solution to identify Generic Columns of excel(xls) and map to Sqlserver Table correctly.
The only solution I know of is to load the data excel data into raw tables - stage this data then qc it with queries etc.
Once you are happy with the integrity of the data you can then load it into your database.
Basically this amounts to a datawarehousing system - you will need human input between the staging and load to warehouse. This is where the qc comes into effect.
All data that comes from a client needs to be qc'd with a human eye as invariably there will be data that breaks your business logic.
Don't trust client data, especially if it is in Excel format!
If any data is incorrect then the standard procedure is to ask the client to resend the corrected data - it should never be the software or receivers job to 'correct' raw data, since as soon as you 'correct ' the data you then become responsible for its contents.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
Ravikiran72p wrote: Is there any solution in Sqlserver or ssis or .NET solution to identify Generic Columns of excel(xls) and map to Sqlserver Table correctly.
Create an "adaptor" (concept rather than a specific type of implementation) specific to each vendor. I suggest STRONGLY that you use extensive error checking on such an adaptor because that is the only way you will detect when they change the format.
The vendor files are delivered to a vendor specific folder and that is used as a key to determine which adaptor is used to process them.
The adaptor could be written in either SQL or C#. I would choose C# because it provides more flexibility for this sort of operation. Note that a C# solution need not go directly to the database. In can just output a file, with a fixed known format, which is then used as an import file to SQL Server.
The adaptors should also have error reporting, logging is good, to report on failed conversion attempts, as well as when successful ones ran.
|
|
|
|
|
How can I backup my db through the terminal, what is the command?
|
|
|
|
|
Hi All,
I am trying to run this query on my database and I am getting an error Incorrect syntax near the keyword 'JOIN'.
USE IVRCallCenterTest
Go
SELECT *
FROM dbo.OutputPromptItem WHERE SegmentId=7198
JOIN dbo.OutputPrompt
ON dbo.OutputPromptItem.PromptId=dbo.OutputPrompt.PromptID
Go
what is wrong with my code ?
Thanks in advance
|
|
|
|
|