enter image description hereSo I created a small SP which is working fine, however I would like to be able to make it ask you to select a start and end date when executing it, thus far I have had no joy in working it out.
Below is my query, could someone please advise me what I need to do in order to force you to select both the start and end date when executing?
I have tried a hundred different #set date commands but am not winning (I am not a pro at this but leaning as I go along).
also attached is a screenshot of what I see when executing the SP, there is nothing parameters which is what I am struggling with.
Thanks!
USE [CBS_AFRICA_LIVECOPY]
GO
/****** Object: StoredProcedure [dbo].[usp_LOADINGHOURS] Script Date: 25/01/2019 19:54:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_LOADINGHOURS]
AS
SELECT FOLIO_NUMBER, STATUS, FORMAT(TERM_START_LOAD_TIME ,'HH') as TERM_START_LOAD_TIME, FORMAT(TERM_END_LOAD_TIME ,'HH')
as TERM_END_LOAD_TIME,TERMINAL_NAME, TERMINAL_ID FROM ORDERS
JOIN TERMINAL_OWNER ON ORDERS.LOADING_TERMINAL_ID = TERMINAL_OWNER.TERMINAL_ID
ORDER BY FOLIO_NUMBER DESC
You may want to try this
ALTER PROCEDURE [dbo].[usp_LOADINGHOURS]
#startdate as datetime,
#enddate as datetime
AS
Begin
SELECT FOLIO_NUMBER, STATUS,
FORMAT(TERM_START_LOAD_TIME ,'HH') as
TERM_START_LOAD_TIME,
FORMAT(TERM_END_LOAD_TIME ,'HH') as
TERM_END_LOAD_TIME,
TERMINAL_NAME,
TERMINAL_ID
FROM ORDER JOIN TERMINAL_OWNER ON
ORDERS.LOADING_TERMINAL_ID =
TERMINAL_OWNER.TERMINAL_ID
Where DBTABLE.DATE>=#startdate
AND DBTABLE.DATE<=#enddate
ORDER BY FOLIO_NUMBER DESC
End
Related
I have these data.
I just want to see the planned order per line per day
so lets say the report is 1 day
1 day is 7u30 till 23h
so in there we will see what is planned for the day
my script is like this:
GO
/****** Object: StoredProcedure [dbo].[LREP_PLANNING_PER_DAY] Script Date: 8/3/2017 2:48:56 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
-- =============================================
-- Description: <Return The get Selections for Reports ,,>
-- =============================================
/*
ALTER PROCEDURE [dbo].[LREP_PLANNING_PER_DAY]
#DATE AS DATETIME
AS
*/
BEGIN
SET NOCOUNT ON
/* TESTING
DECLARE #DATE DATETIME
SET #DATE = '2017-05-09'
*/
SELECT
STR_ORDER_ID
,DT_ESTIMATED_STARTTIME_UTC
,DT_ESTIMATED_ENDTIME_UTC
,STR_EQUIPMENT_LABEL
FROM [dbo].[lFp_Orders]
WHERE STR_ORDER_STATUS='Planned'
AND (DT_ESTIMATED_STARTTIME_UTC between #DATE + '07:30:00' and #DATE + '23:00:00')
END`
I did my report design like this:
but in preview I couldn't see my order. Can you help me this issue?
I want to see my all order for line in one day.
but my order interval must be depend on between end time and start time.
Thanks.
I tried myself and If you have a problem with gantt chart you can create bar chart after that you can change chart type. I fixed my issue like this.
I am trying to implement a search functionality using JSP and SQL Server. I am required to search using keywords such a phone number. But my lecturer wants me to implement the search functionality a bit differently.
Let's say - I have a phone number in the database 123456 and I key in the number in the search box 123. The procedure should be able to retrieve all the records with the phone number that starts with 123.
I tried the following so far but I have to enter the full number for the results to be retrieved. How can I change my procedure so that it works the way my lecturer wants?
USE [test]
GO
/****** Object: StoredProcedure [dbo].[add_data] Script Date: 15-11-2016 10:14:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [dbo].[select_data]
(
#thePhoneNumber VARCHAR(200),
#theName VARCHAR(200) OUT
)
As
Begin
SELECT #theName= Name FROM real WHERE PhoneNumber=#thePhoneNumber
End
I don't know if this is the answer you are looking for but for a normal SQL query you should use the % sign every time you want to search the rest.
For example in this context you should use:
SELECT phonenumber FROM real WHERE phonenumber LIKE '123%'
Hope it Helps :)
Like operator will help you in this case.
USE [test]
GO
/****** Object: StoredProcedure [dbo].[add_data] Script Date: 15-11-2016 10:14:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [dbo].[select_data]
(
#thePhoneNumber VARCHAR(200),
#theName VARCHAR(200) OUT
)
As
Begin
SELECT #theName= Name FROM real WHERE PhoneNumber like '#thePhoneNumber+'%'
End
I'm currently facing a strange situation.
I'm collecting code of existing stored procedures from a query to a TMP table.
TABLE:
##SPListAndCode
(
Code nVarchar(MAX)
)
Query:
INSERT INTO ##SPListAndCode
SELECT OBJECT_DEFINITION (OBJECT_ID('SPname')))
After that I am trying to replace values to get from Create query, Alter query
REPLACE(CODE, 'CREATE PROCEDURE', 'ALTER PROCEDURE')
But problem is this: REPLACE function is not replacing values.
But, when I am trying to use
REPLACE(CODE, 'CREATE', 'ALTER')
function works as expected.
But this scenario are not acceptable for me, because inside the stored procedure there can be things like
CREATE TABLE
Example data inside "Code" column:
/****** Object: StoredProcedure dbo.spName Script Date: 6/20/2016 9:10:18 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE dbo.spName
AS
DECLARE #pStartDate date, #x int
SET #pStartDate = (SELECT max(CT_ACTIVITY_DATE) FROM Table)
...
Thanks a lot in advance for any kind of support!
Your stored procedure has two spaces between CREATE and PROCEDURE, while your replace is looking for the string with a single space between the words.
To gain access to the actual code contained inside of the stored procedures, you can use something like this:
SELECT
so.name [ObjectName], so.type,
OBJECT_NAME(sc.id), sc.id, sc.colid , sc.[text]
FROM
sys.syscomments sc
INNER JOIN
sys.sysobjects so ON so.id = sc.id
WHERE
so.type = 'P'
ORDER BY
sc.id, sc.colid
Note there can be multiple entries for each object, and the colid is used to order those entries.
I am completely new to SQL Server and a bit lost. When I try the following, it executes the first three lines and ignores the rest, just get
'Command(s) completed successfully.'
USE [RenewalsDb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[One]
AS
BEGIN
SET NOCOUNT ON;
DROP TABLE [dbo].NewTransTable;
SELECT * INTO [dbo].[NewTransTable] FROM [dbo].aqryTransTable;
DELETE FROM [dbo].[NewTransTable] WHERE (((NewTransTable.CURRENT_LICENSE_SKU_DESC) Like '% partner program %'));
DELETE FROM [dbo].[NewTransTable] WHERE (((NewTransTable.RENEWAL_MAINTAINANCE_SKU_DESC) Like '% partner program %'));
UPDATE NewTransTable SET NewTransTable.[Quote Number] = Null;
UPDATE dbo.TransactionTable SET Concat = dbo.qryNamedAcReseller.LATEST_DISTRIBUTOR_NAME + dbo.qryNamedAcReseller.[Sub Territory FY14 ]
FROM dbo.TransactionTable INNER JOIN
dbo.qryNamedAcReseller ON dbo.TransactionTable.LATEST_INSTANCE_NUMBER = dbo.qryNamedAcReseller.LATEST_INSTANCE_NUMBER;
UPDATE dbo.TransactionTable SET Concat = dbo.qryNamedAcReseller.[Sub Territory FY14 ]
FROM dbo.TransactionTable INNER JOIN
dbo.qryNamedAcReseller ON dbo.TransactionTable.LATEST_INSTANCE_NUMBER = dbo.qryNamedAcReseller.LATEST_INSTANCE_NUMBER
WHERE Concat IS NULL;
UPDATE dbo.NewTransTable SET [Quote Number] = dbo.Autogen.[Quote Number] FROM dbo.Autogen RIGHT OUTER JOIN
dbo.NewTransTable ON dbo.Autogen.[IB Reference Num] = dbo.NewTransTable.LATEST_INSTANCE_NUMBER AND
dbo.Autogen.[Quote Known As] = dbo.NewTransTable.[Quote Known As]
DROP TABLE [dbo].NewTransTable2;
SELECT * INTO [dbo].[NewTransTable2] FROM [dbo].aqryTransTable2;
ALTER TABLE [dbo].NewTransTable2 ADD Named_Account nvarchar(255);
END
GO
Welcome to stackoverflow.
Stored Procedure is like a template which can reused multiple times and it can be made dynamic with the help or parameters. Refer mssqltips.com - SQL Server Stored Procedure Tutorial on guide to Stored Procedures. If you want to execute some commands only once, Stored Procedure is not the right thing.
So when you execute the above script, what SQL Server is doing is creating the template structure i.e. Stored procedure named [One] and not actually running the scripts within the stored procedure.
To execute this stored procedure named [One] you have to call it using EXEC One or just simply One and Execute (F5)
I am not an sql Guru, that's why I'm asking you guys.
I have this stored procedure:
USE [groep2_festivals]
GO
/****** Object: StoredProcedure [dbo].[GetGroupsNotOfFestival] Script Date: 15-05-13 10:03:17 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Robbie Vercammen
-- Create date: 2013-05-15
-- Description: Getting all groups not on a defined festival
-- =============================================
ALTER PROCEDURE [dbo].[GetGroupsNotOfFestival]
#festId nvarchar(4)
AS
BEGIN
SELECT DISTINCT b.*, p.*
FROM bands b
JOIN bandsperfestival bpf ON b.band_id = bpf.band_id
JOIN podia p ON p.pod_id = bpf.pod_id
WHERE bpf.fest_id != #festId
END
And these tables,
Bands:
Podia (Stages in english):
BandsPerFestival holding all bands that perform on festivals:
I used this stored procedure to get all bands performing on a specific festival (by festival id) and that works, but I would like to invert this but simple replacing WHERE bpf.fest_id = #festId with WHERE bpf.fest_id != #festId doesn't seem to work. I get the same list but the bands that actually do perform on a festival are returned twice.
Example:
As you can see, nickelback appears in the list twice when they are actually performing on the chosen festival.
Any help is appreciated.
EDIT: I should have said this earlier, but I want all the bands that are not performing on a specific festival,
Do a left outer join, and then forced the right side of the join to be null.
SELECT DISTINCT b.*
FROM bands b
left outer JOIN bandsperfestival bpf ON b.band_id = bpf.band_id
and #festId = bpf.fest_id
where bpf.fest_id is null
You'll need to explain what you mean when you say "Invert" this. Do you mean you want all bands that did NOT play any festival? Or All bands that did not play a specific festival?