Implementing search criteria using JSP and SQL Server 2008? - sql-server

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

Related

Selection Criteria when running an SP

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

How to perform an IF/ELSE check when creating a view

I'm writing a script to create a view, only IF the view does not already exist. If the view does already exist, I don't want to alter it or drop and re-create it. The syntax below is obviously not complete, and generates an error because CREATE VIEW needs to be in its own batch - but what is the proper way to construct my use case?
IF OBJECT_ID('dbo.view_name') IS NULL
BEGIN
CREATE VIEW [dbo].[view_name]
AS
SELECT ...;
END
ELSE
...
SQL Server 2016 has CREATE OR ALTER.
CREATE OR ALTER VIEW vw_your_view
AS
SELECT 1 FROM your_Table
GO
This will blow up if you move it to an environment below SQL Server 2016. If that is the case, go with what you have and check for an obj ID.
I changed the SQL to be the following. To avoid the "CREATE VIEW must be in its own batch" error, I wrapped the "CREATE VIEW" inside an exec('') statement. This works!
USE [XXXXXXXXX]
GO
/****** Object: View [CXXXXXXX].[_MobileMessageTracking_v2] Script Date: 5/4/2018 3:19:04 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF Object_id('CXXXXXXX._MobileMessageTracking_v2') IS NULL BEGIN
exec('CREATE VIEW [CXXXXXXX].[_MobileMessageTracking_v2]
AS
SELECT
/* fields here */
FROM CXXXXXXXX._MobileMessageTracking AS M WITH (NOLOCK)
WHERE M.MID = XXXXXXX
AND M.CreateDateTime >= DATEADD(mm,-6, GETDATE())
UNION
select
/* fields here */')
END
GO

do i need to explicitely add SET ANSI_NULLS ON in EntityFramework migration

I am writing migration for stored procedure with EF 6
string sqlQuery = #"
/****** Object: StoredProcedure [dbo].[GetSalesByLocation] Script Date: 9/21/2016 8:32:36 AM ******/
SET ANSI_NULLS ON GO
SET QUOTED_IDENTIFIER ON GO
CREATE PROCEDURE [dbo].[GetSalesByLocation]
#MerchantID bigint,
#LocationID bigint,
#AccountAccessID bigint,
#KioskID bigint,
#StartDate DATETIME,
#EndDate DATETIME
AS
BEGIN
.....
END
GO";
Sql(sqlQuery);
when i try to run Update-database command i get below error
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.
Incorrect syntax near 'GO'.
Also when i create SP without
SET ANSI_NULLS ON GO
SET QUOTED_IDENTIFIER ON GO
and open in SQL management studio i can see above statement getting added automatically.
So my question is if i am writing migration for stored procedure do i need to `set ansi null on explicitly?
SET ANSI_NULL and SET QUOTED_IDENTIFIER outside the procedure affects SQL Server behaviour only for the session (so it will not be written in the stored procedure and it will not affect the stored procedure behaviour when the stored procedure will be run).
Also, SET ANSI_NULL ON is the default (try SELECT GETANSINULL() on SQMS) so you could omit it. You can also omit SET QUOTED_IDENTIFIER ON (you are using [] to quote identifiers and not ").
If you need to run more statements on the same connection, in Management Studio you use GO, using ADO you can split them on different execution methods (so you will eventually see errors in the right place). Otherwise you can use ; (in this case probably you can't use ;).

How can I do if else conditional statement in where cluse

Please check my current stored procedure.
USE [CastingDatabase]
GO
/****** Object: StoredProcedure [dbo].[Searching_Talents] Script Date: 11/11/2015 1:41:19 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Searching_Talents]
#name nvarchar(100),
#age_from nvarchar(10),
#age_to nvarchar(10)
AS
BEGIN
SELECT *
FROM [dbo].[Talents]
WHERE(
((#name is null) or (#name='') or ([dbo].[Talents].[Name] LIKE '%'+#name+'%'))
AND
(((#age_to is null) or (#age_to='') or (DATEDIFF(YEAR,Talents.DOB,GETDATE()) IN
(
SELECT CAST (Item as int) FROM dbo.SplitString(#age_to,',')
)
))
)
)
ORDER BY Name
END
I want to add some if_else condition statements in second where clause.
How can I add?
I want to add like this
AND(if(#age_from is null){---- my codes ----})
P.S. I am a newbie in stored procedure. Please help me.
Add CASE where you want to check some value. This doesn't apply only for procedures, it applies on all queries in sql server
...
AND (
CASE
WHEN #age_from IS NULL
THEN yourCode
END
)
or if you have only to check if null and write something else, you can check it like this:
...
AND (ISNULL(#age_from, defaultValue)
and in this case, if value of age_from is null, sql server will put defaultValue which you state in above code.

An error on create a StoredProcedure

I want to create StoredProcedure like this :
USE [banktest]
GO
/****** Object: StoredProcedure [dbo].[UpdatePayment] Script Date: 8/21/2015 8:22:00 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[UpdatePayment] #status nvarchar(50),#RefID nvarchar(50),#SaleReferenceId nvarchar(50),#paymentId int
AS
BEGIN
update bankpayment set [status]=#status,RefID=#RefID,SaleReferenceId=#SaleReferenceId where paymentid=#paymentId
END
But I have an error like this
I tried to change that name but It doesn't work .
What should i do?
Try
CREATE PROCEDURE
instead of
ALTER PROCEDURE
Follow THIS LINK for more details.
That is not an error. Just the cache is not created for that object. Please compile and I am sure you wont get any error.

Resources