I am trying to get all the SQL caseID between two datetimes - sql

I am building a webservice in ASP.NET that will call a stored procedure to get all the cases between two dates. I am using SQL Management Studio Here is my code:
ALTER PROCEDURE spGetNewCases
(#startDate datetime, #endDate datetime)
AS
BEGIN
SELECT
caseID
FROM
tblCases
WHERE
dateOpened BETWEEN #startDate and #endDate
END
GO
EXEC spGetNewCases
However, I get the following error:
"Could not find object spGetNewCases".
In the IDE , the words "between" and "and" are highlighted in light gray instead of dark blue. Moreover, I would like to test it with two dates, say 2015-01-01 and 2015-01-04.
The format of my datetime in my SQL server is datetime2((0),null). E.g.
2015-01-02 00:00:00.
I would greatly appreciate the community's feedback. Thank you!
EDIT:
Thank you everyone for your great help! Here is my new code that works!
-- procedure
USE [vetDatabase_Wizard]
GO
ALTER PROCEDURE sp_GetNewCases (#startDate datetime, #endDate datetime)
AS
BEGIN
SELECT caseID FROM tblCases
WHERE dateOpened BETWEEN #startDate and #endDate
END
GO
EXEC sp_GetNewCases '2015-01-01', '2015-04-04'

Check your database
USE CASE_DB
GO
Qualify your schema name.
ALTER PROCEDURE schema.spGetNewCases ...
See if that doesn't find it...
EDIT
IF you're using SSMS -- right-click your stored procedure and select 'modify'. That will give you a pre-written ALTER template that you can then modify.

If your SP is created then check your DB context. In SSMS there is a dropdown menu, you can also USE DBNAME. In ASP code it is in your connection string.
If it is not created you will need to Create Procedure first and then you can Alter Procedure.
You can call the SP like so : EXEC spGetNewCases '2016-03-01', '2016-05-01'
Side note: System Stored Procedures use the sp prefix you probably should choose something different like sp_ or usp.

Related

How to track changes in development environment in SQL Server

Our development team works on SQL Server and writes stored procedures for our product.
We need something like a version control system for those procedures or any other objects.
Sometimes I change a stored procedure, and someone else in my team changes it and I don't know any thing about it.
Is there any solution for that?
If you want to do it via code you could run this on a daily or hourly basis to get a list of all procs that were changed in the last day:
select *
from sys.objects
where datediff(dd, create_date, getdate()) < 1
or datediff(dd, modify_date, getdate() < 1)
and type = 'P';
or you could create a ddl trigger:
Create trigger prochanged On database
For create_procedure, alter_procedure, drop procedure
as
begin
set nocount on
Declare #data xml
set #data = Eventdata()
-- save #data to a table...
end
This will allow you to save all kinds of information every time a proc is created, changed or deleted.

Several stored procedure problems

I just started working with stored procedures. I have 2 problems.
When I create an object with a new name, it works successfully BUT no table shows up... where is it?
Second when I execute again I get an error
There is already an object named '##sp_MemberCertificates' in the database
I'm trying to fix this by using:
IF (SELECT object_id('TempDB..##Temp')) IS NOT NULL
BEGIN
DROP TABLE ##Temp
END
But I don't know how to use this exactly...
CREATE PROCEDURE ##sp_MemberCertificates (#MemberId int, #FromDate Varchar (30), #ToDate Varchar (30))
AS
SELECT Email,
CertificateNumber,
Mem_Name,
Mem_Address,
Mem_city,
Mem_state,
Mem_cellno,
Amount,
DateValid,
DateSent
--Use INTO ?? --
FROM UnitedDiningClub.dbo.UDC_Certificates
JOIN UnitedDiningClub.dbo.UDC_Member m
ON m.Mem_ID = UDC_Certificates.MemberId
JOIN UnitedDiningClub.dbo.UDClub_Client c
ON c.ClientId = UDC_Certificates.RestaurantId
WHERE MemberId = #MemberId
ORDER BY MemberId
EXEC ##sp_MemberCertificates #MemberId = '1', #FromDate = '2011-07-19 00:00:00.000', #ToDate = '2015-07-19 00:00:00.000'
go
Thanks for the help, please let me know if there more problems with the code, I am trying to learn.
use this to execute your stored procedure
rename your procedure to usp_MemberCertificates '##sp_MemberCertificates' is used for temp sp
remove exec statement from your procedure
use exec statement like this
EXEC usp_MemberCertificates 1, '2011-07-19 00:00:00.000', '2015-07-19 00:00:00.000'
you don't have to mention the variable name
moreover 1 is integer whereas you were sending a varchar type
also once you create a procedure you should add Alter instead of Create to so that it doesnt throw this error
There is already an object named '##sp_MemberCertificates' in the
database
You create global temporary stored procedure (using prefix ##). This stored procedure is visible from all the sessions in the server. The stored procedure is in Temp DB (as you already mentioned). So you can add DROP PROCEDURE, not drop table as in your example. Also you have to DROP ##sp_MemberCertificates, not ##Temp.
Second when creating procedure the script must finish with GO, but before you try to execute (EXEC ##sp_MemberCertificates #MemberId = '1', #FromDate = '2011-07-19 00:00:00.000' in your code). Also if you intend to use INTO (--Use INTO ?? -- in your code) you must drop table before that.

SQL Variables Change in SSIS

I'm new to SSIS so forgive me if this question is trivial or has already been answered. So I have a SQL query that begins as follows:
declare #start datetime, #end datetime, #startMonth datetime, #endMonth datetime, #maxHoursToRespond int
----Set These------------------------------------------------
set #end='6/27/2014'
set #maxHoursToRespond=24
-------------------------------------------------------------
set #start=dateadd(dd, -90, #end) -- set duplication period
set #startMonth=dateadd(dd, -2, #end)-- set to start date of output you want
set #endMonth=dateadd(dd, -1, #end) -- set to day of end date of output you want
When I put this in my OLE DB Source Editor with SQL command as my data access mode,
all the variables are replaced with question marks.
It looks like :
DECLARE ? datetime, ? datetime, ? datetime, ? datetime, ? int
/*--Set These------------------------------------------------*/ SET ? = ?
SET ? = 24
/*-----------------------------------------------------------*/ SET ? = dateadd(dd, - 90, ?)
SET ? = dateadd(dd, - 2, ?)
SET ? = dateadd(dd, - 1, ?)
In the query builder.I'd like to know why this is happening.I'd also like to know how I can allow the query to be successfully built
(currently I get a syntax error of "The Declare SQL construct or statement is not supported.").
Do I have to create these variables (like #start) in SSIS itself?
You can:
Encapsulate your SQL in a stored procedure that contains all of the declarations internally (assuming the variable values are static), then call the stored procedure in the execute SQL task with EXEC. EXEC My_Stored_Procedure
Write a stored procedure that accepts the variables as inputs, map them to variables in SSIS, then execute the stored procedure like this Exec My_Stored_Proc ?,?,?,? with the user variables mapped to the corresponding stored procedure variables.
Leave the query as is, but remove the DECLARE and the SETs, and map the SSIS variables to the query. This most likely will not work, because SSIS will not know which ? corresponds with which variable (it will try to map them in the order they appear.
Number 2 is the generally accepted method, unless you store your variable values in a table or something that the SP in number can access, in which case number 1 may be cleaner.
I have pasted straight to SQL command text yours script in OLE DB Source Editor and nothing changed in it. By pressing Parse Quesry.. I checked that SQL is correct. When I tried to use Build Query.., it said, that DECLARE is not supported. So don't use builder :)

declare variables in sql ce

I am tryin to get records from a table within a month in sql compact edition.
Here is the sql query I know:
DECLARE #startDate as DATETIME, #EndDate as DATETIME
#startDate = GetDate();
#ENdDate = DATEADD(m,1,#startDate)
select * from table where (columnname between #startdate and #enddate)
I know that you have to send one script at a time, but how can you declare variables in sql ce(I guess it doesn't accept declare)?
I think my answer is very late for this question, but hope it will be useful for someone.
You can't declare some variable in SQL CE, because just one statement can be used per command. As ErikEJ said in this link.
You need to refactor your script to one big statement, if it's possible!
I will be very happy to hear a better solution.
Take a look at post How do I populate a SQL Server Compact database?
and see if tool referenced there may help you.
If you call it through an application (I'm not sure how you read the data)
Prepare your query just like this:
select * from table where (columnname between ? and ?)
but I'm not sure if you can use the between keyword. may be you need to change this.
then you need to add your SqlCeParameter objects like this:
cmd.Parameters.Add(new SqlCeParameter("p1", SqlDbType.DateTime, myDate));
I'm not familiar with SQL-CE, but I think you're missing some Set statements. Try this:
DECLARE #startDate as DATETIME, #EndDate as DATETIME
Set #startDate = GetDate();
Set #ENdDate = DATEADD(m,1,#startDate)
select * from table where (columnname between #startdate and #enddate)
Update
See the Using Parameters in Queries in SQL CE reference from MSDN. You are correct in that Declare is not a valid keyword, so you'll need to the query as a parameterized version from the application itself.
select * from table where (columnname between ? and ?)

SQL Server: problem with UDF

I have a function and select statement
CREATE FUNCTION dbo.fun_currentday ( #dt DATETIME)
RETURNS DATETIME
AS
BEGIN
DECLARE #currentday DATETIME
SET #currentday = DATEADD(dd, DATEDIFF(dd, 0, #dt), 0)
RETURN (#currentday)
END
GO
DECLARE #pvm AS DATETIME
SET #pvm = GETDATE()
SELECT 'Last 7 days' AS Range, dbo.fun_currentday(#pvm) - 6 AS Stday, dbo.fun_currentday(#pvm) AS Endday
All works fine but when I hover over dbo.fun_currentday at the select statement, I get an error saying:
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.fun_currentday", or the name is ambiguous.
Where's the problem?
Intellisense / Error highlighting always does this for newly created objects. Use Ctrl+Shift+R to refresh the local cache.
Before
After
Everything runs fine here with SQL Server 2008 Express.
Have you tried running your query on a different database?
You can try to create a new schema and create you UDF in it.
Make sure you have the necessary permissions and that the dbo schema configuration is correct.
Your stored procedure have to be stored somewhere so when you don't specify the location it goes to default database (master). So you should call it like
SELECT 'Last 7 days' AS Range,master.dbo.fun_currentday(GETDATE()) - 6 AS Stday, master.dbo.fun_currentday(GETDATE()) AS Endday
EDITED
I've checked that and I wasn't right, it not always goes to mater schema. It goes to database in contex of you were woring, so if your create procedure was created in query on root folder it goes to master, but if you created it in query of test databse you should use test.dbo.fun_currentday(GETDATE()). To avoid it always specify the databse like USE database_name GO CREATE FUNCTION dbo.fun_currentday