An error on create a StoredProcedure - sql-server

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.

Related

how to access a column of a table via foreign key?

I am new in SQL server and having trouble with this trigger code. Can you please tell me what is wrong with this line: set NazivFirme=(select NazivFirme from inserted)... This is where I get an error.
USE [BazaPreduzece]
GO
/****** Object: Trigger [dbo].[promenaNazivaFirme] Script Date: 2017-03-28 5:29:47 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER trigger [dbo].[promenaNazivaFirme]
on [dbo].[Firma]
AFTER UPDATE
as
declare #FirmaID int
select #FirmaID=FirmaID from inserted
if update (NazivFirme)
begin
alter table Vlasnik disable trigger zabranaAzuriranjaNazivaFirme
update [dbo].[Vlasnik]
set NazivFirme=(select NazivFirme from inserted)
where FirmaID=#FirmaID
alter table Vlasnik enable trigger zabranaAzuriranjaNazivaFirme
end
That's theFirma and the Vlasnik tables...

Implementing search criteria using JSP and SQL Server 2008?

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

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 ;).

SQL Server 2005 - The multi-part identifier could not be bound

This code:
USE [db]
GO
/****** Object: UserDefinedFunction [dbo].[GetFieldPickerReports] Script Date: 01/11/2013 19:12:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[GetFieldPickerReports] (#CustomerName varchar(100))
RETURNS TABLE
AS
RETURN SELECT [fpr].[ID], [fpr].[Name], [fpr].[Global], [fpr].[FPRCategoryID]
FROM FieldPickerReport fpr, dbo.GetProductKeyIdByCustomer(#CustomerName) pk
LEFT JOIN [FPRCategory] fprcat on ([fpr].[FPRCategoryID]=[fprcat].[ID])
WHERE [Global]=1 OR ProductKeyID=pk.id
Produces an error:
The multi-part identifier "fpr.FPRCategoryID" could not be bound.
This query works without the Left Join (which I currently need to add) or either with the removal of dbo.GetProductKeyIdByCustomer() and the Where clause.
Here's the code for dbo.GetProductKeyIdByCustomer()
USE [db]
GO
/****** Object: UserDefinedFunction [dbo].[GetProductKeyIdByCustomer] Script Date: 01/11/2013 19:58:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[GetProductKeyIdByCustomer] (#CustomerName varchar(100))
RETURNS TABLE
AS
RETURN SELECT id
FROM ProductKey
WHERE [CustomerName]=#CustomerName AND Enabled=1
What is wrong with my query?
First, I think that these UDFs would be better implemented as Views.
Second, you are mixing implicit and explicit joins. Using all explicit joins should solve your issue:
SELECT
[fpr].[ID],
[fpr].[Name],
[fpr].[Global],
[fpr].[FPRCategoryID]
FROM
FieldPickerReport fpr
JOIN dbo.GetProductKeyIdByCustomer(#CustomerName) pk
ON [Global] = 1 OR ProductKeyID = pk.id
LEFT JOIN FPRCategory fprcat
ON fpr.FPRCategoryID = fprcat.ID

alter stored procedure to use snapshot isolation level

Quick question I have a database with snapshot enabled using
ALTER DATABASE myDB
SET ALLOW_SNAPSHOT_ISOLATION ON
I'm trying to alter an existing stored procedure to use the transaction isolation level to read uncommitted using as follows:
USE [myDB]
GO
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
GO
/****** Object: StoredProcedure [dbo].[myStoredProcedure] Script Date: 03/27/2012 11:39:24 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[myStoredProcedure]
AS
BEGIN
SELECT *
FROM someTable
END
RETURN 0
But when I reopen the stored procedure the SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED statement is gone.
USE [myDB]
GO
/****** Object: StoredProcedure [dbo].[myStoredProcedure] Script Date: 03/27/2012 11:39:24 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[myStoredProcedure]
AS
BEGIN
SELECT *
FROM someTable
END
RETURN 0
Was the transaction level actually set? I expected the statement to still be there after closing and reopening the stored procedure window. Just wanted to verify, thanks.
You have to put it in the procedure body. If it is outside you are just altering it in that isolation level as opposed to altering the procedure definition to use it.
USE [myDB]
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[myStoredProcedure]
AS
BEGIN
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT *
FROM someTable
END

Resources