Interesting one - difficult to pin this one down. Have you thought about looking at the public role?
sp_helprotect 'CREATE PROCEDURE',NULL,NULL,'s'
Does that bring you back anything?
Not sure where you copied this code from, but it looks like it came from a web site that translated minus signs (-
) to long dashes (–
). You can see this in Management Studio if you turn IntelliSense on:
Here is the easy way to compare the same range as the year before:
DECLARE @startdate date = '20170101',
@enddate date = '20170212';
SELECT @startdate, DATEADD(YEAR, -1, @startdate),
@enddate, DATEADD(YEAR, -1, @enddate);
This compares the same period as last year, but I'm not sure that's what you're after since you are also using the zero date and datediff
- not clear why. It does not account for leap year; you'll need to define what should happen if the range involves 2/29 either this year or last year.
When you use those dates in a query, don't do this subtracting 3 milliseconds nonsense - it only leads to problems. If you're trying to get all the data for February 12th even if it has time, then use < DATEADD(DAY, 1, @enddate)
. Also, don't use YY
, just type out YEAR
. It is also far easier to get the full range of last year:
-- for SQL Server 2012+
WHERE col >= DATEFROMPARTS(YEAR(@startdate)-1, 1, 1)
AND col < DATEFROMPARTS(YEAR(@startdate), 1, 1);
-- for older, unsupported versions:
WHERE col >= DATEADD(YEAR, YEAR(@startdate)-1901, '19000101'),
AND col < DATEADD(YEAR, YEAR(@startdate)-1900, '19000101');
To sum up:
DECLARE @startdate date = '20170101',
@enddate date = '20170212';
-- sales from that period this year:
SELECT SUM(some_column) FROM dbo.SomeTable
WHERE some_datetime_column >= @startdate
AND some_datetime_column < DATEADD(DAY, 1, @enddate);
-- sales from same period previous year (again, not accounting for leap special case):
SELECT SUM(some_column) FROM dbo.SomeTable
WHERE some_datetime_column >= DATEADD(YEAR, -1, @startdate)
AND some_datetime_column < DATEADD(YEAR, -1, DATEADD(DAY, 1, @enddate));
-- sales from all of previous year:
SELECT SUM(some_column) FROM dbo.SomeTable
WHERE some_datetime_column >= DATEADD(YEAR, YEAR(@startdate)-1901, '19000101'),
AND some_datetime_column < DATEADD(YEAR, YEAR(@startdate)-1900, '19000101');
Suggested reading:
Best Answer
You can't change the ability to create procedures to only one schema by only granting permissions on the schema. (Assuming the user has no other rights.)
Why?
The user still needs the right to create objects in the database, which are in this case procedures.
What you can do, is grant the user
CREATE PROCEDURE
rights, and then either change the owner of the schema to that user (more secure, see below for more information) or grant that user permissions on the 'IC'SCHEMA
.Only granting
CREATE PROCEDURE
does not allow the user to create procedures on schema's such as dbo.Create the login, the corresponding user and the IC schema
Grant Create procedure to the user
Either Change the owner of the schema (Safer Option)
Or grant the user access to the schema
Risk involved with granting rights on the schema
Source
Take a look at the answer by @DavidBrowne-Microsoft for the origin of the quote, and to get more information on "ownership chaining" security risks when granting alter schema rights.
Test
Result:
This fails
Undo impersonation