Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Msg 2786 on SP

Author  Topic 

sas0riza
Yak Posting Veteran

56 Posts

Posted - 2008-11-17 : 11:52:01
What does this error mean?

--Msg 2786, Level 16, State 1, Procedure sp_GetMovieInfo, Line 61
--The data type of substitution parameter 1 does not match the expected type of the format specification.





create proc sp_GetMovieInfo
@Title varchar(100) = null,
@GenreID int = null,
@ReleaseYear = null

if exists (select * from Movies where Title like '%@Title%')
begin
select Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStock
from Movies, Genre, Rating
where Title like '%@Title%' and Movies.GenreID = Genre.GenreID and Movies.RatingID = Rating.RatingID

return 0
end



visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-17 : 12:22:18
not sure about error message. but your posted sp has couple of errors. it should be

create proc sp_GetMovieInfo
@Title varchar(100) = null,
@GenreID int = null,
@ReleaseYear int= null

if exists (select * from Movies where Title like '%'+@Title+ '%')
begin
select Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStock
from Movies
inner join Genre
on Movies.GenreID = Genre.GenreID
inner join Rating
on Movies.RatingID = Rating.RatingID

where Title like '%'+@Title+ '%'
return 0
end


Also learn to use ANSI joins as other syntax wont be supported in future versions of SQL Server
Go to Top of Page

sas0riza
Yak Posting Veteran

56 Posts

Posted - 2008-11-17 : 12:28:03
quote:
Originally posted by visakh16

not sure about error message. but your posted sp has couple of errors. it should be

create proc sp_GetMovieInfo
@Title varchar(100) = null,
@GenreID int = null,
@ReleaseYear int= null

if exists (select * from Movies where Title like '%'+@Title+ '%')
begin
select Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStock
from Movies
inner join Genre
on Movies.GenreID = Genre.GenreID
inner join Rating
on Movies.RatingID = Rating.RatingID

where Title like '%'+@Title+ '%'
return 0
end


Also learn to use ANSI joins as other syntax wont be supported in future versions of SQL Server



Thanks! The error probably stemmed from what you corrected, '%'+@Title+ '%'.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-17 : 12:33:39
ok. between whats the purpose of @ReleaseYear & @GenreID? i cant see where they are used. If not needed, remove them.
Go to Top of Page

sas0riza
Yak Posting Veteran

56 Posts

Posted - 2008-11-17 : 12:36:12
quote:
Originally posted by visakh16

ok. between whats the purpose of @ReleaseYear & @GenreID? i cant see where they are used. If not needed, remove them.



I need them too because the SP will run a select depending upon which input parameter is given.

Here's my other 2 pieces of code:

if exists (select * from Movies where GenreID = @GenreID)
begin
select Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStock
from Movies, Genre, Rating
where Movies.GenreID = @GenreID and Movies.GenreID = Genre.GenreID and Movies.RatingID = Rating.RatingID

return 0
end


if exists (select * from Movies where ReleaseYear = @ReleaseYear)
begin
select Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStock
from Movies, Genre, Rating
where Movies.ReleaseYear = @ReleaseYear and Movies.GenreID = Genre.GenreID and Movies.RatingID = Rating.RatingID

return 0
end
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-17 : 12:39:31
there's no need of this if statements you need only the below

create proc sp_GetMovieInfo
@Title varchar(100) = null,
@GenreID int = null,
@ReleaseYear int= null

if exists (select * from Movies where Title like '%'+@Title+ '%')
begin
select Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStock
from Movies
inner join Genre
on Movies.GenreID = Genre.GenreID
inner join Rating
on Movies.RatingID = Rating.RatingID

where (Title like '%'+@Title+ '%' or @Title is null)
and (Movies.GenreID = @GenreID or @GenreID is null)
and (Movies.ReleaseYear = @ReleaseYear or @ReleaseYear is null)
return 0
end
Go to Top of Page

sas0riza
Yak Posting Veteran

56 Posts

Posted - 2008-11-17 : 13:02:21
quote:
Originally posted by visakh16

there's no need of this if statements you need only the below

create proc sp_GetMovieInfo
@Title varchar(100) = null,
@GenreID int = null,
@ReleaseYear int= null

if exists (select * from Movies where Title like '%'+@Title+ '%')
begin
select Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStock
from Movies
inner join Genre
on Movies.GenreID = Genre.GenreID
inner join Rating
on Movies.RatingID = Rating.RatingID

where (Title like '%'+@Title+ '%' or @Title is null)
and (Movies.GenreID = @GenreID or @GenreID is null)
and (Movies.ReleaseYear = @ReleaseYear or @ReleaseYear is null)
return 0
end




Sorry, I should have been more specific. Like I want the SELECT to run based on any 1 input parameter.

I tried what you provided and if I enter a @Title, it works. But if I enter a @GenreID, it doesn't return any results except return value 0. Same with @ReleaseYear.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-19 : 12:12:37
ah...i forgot to remove a bit. try this now and see

create proc sp_GetMovieInfo
@Title varchar(100) = null,
@GenreID int = null,
@ReleaseYear int= null
begin
select Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStock
from Movies
inner join Genre
on Movies.GenreID = Genre.GenreID
inner join Rating
on Movies.RatingID = Rating.RatingID

where (Title like '%'+@Title+ '%' or @Title is null)
and (Movies.GenreID = @GenreID or @GenreID is null)
and (Movies.ReleaseYear = @ReleaseYear or @ReleaseYear is null)
return 0
end



Go to Top of Page

sas0riza
Yak Posting Veteran

56 Posts

Posted - 2008-11-19 : 20:52:52
quote:
Originally posted by visakh16

ah...i forgot to remove a bit. try this now and see

create proc sp_GetMovieInfo
@Title varchar(100) = null,
@GenreID int = null,
@ReleaseYear int= null
begin
select Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStock
from Movies
inner join Genre
on Movies.GenreID = Genre.GenreID
inner join Rating
on Movies.RatingID = Rating.RatingID

where (Title like '%'+@Title+ '%' or @Title is null)
and (Movies.GenreID = @GenreID or @GenreID is null)
and (Movies.ReleaseYear = @ReleaseYear or @ReleaseYear is null)
return 0
end





Yes this works the way I wanted it, thanks for your help!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-20 : 00:26:32
Cheers
Go to Top of Page

sas0riza
Yak Posting Veteran

56 Posts

Posted - 2008-11-20 : 12:01:38
I have another question. I have the following SP. I only want the message 'Video is overdue' to print if the DateDue < GETDATE(). I wasn't sure if I should add another IF statement or if there's an easier way. Thanks,






create proc [dbo].[sp_UpdateRentalRecord]
/* input parameters */
@MemberID int,
@VideoID int


as
begin

-- validate video id
if exists (select 1 from Videos where VideoID = @VideoID)
begin


--impend a late fee charge if video is returned after due date
--the late fee is $1.99 * the number of days after the due date
update Customers
set Balance = Balance + (1.99*DATEDIFF(dd,rr.DateDue,GETDATE()))
FROM Customers c
INNER JOIN RentalRecord rr
ON rr.MemberID = c.MemberID
and rr.DateReturned is null
where c.MemberID = @MemberID
AND DateDue < GETDATE()

print 'Video is overdue'


--add the return date of the video
update RentalRecord
set DateReturned = GETDATE()
where VideoID = @VideoID



update Movies
set TotalInStock = TotalInStock + 1
from Movies, RentalRecord, Videos, Customers
where RentalRecord.VideoID = @VideoID and RentalRecord.MemberID = @MemberID and RentalRecord.VideoID = Videos.VideoID and Videos.MovieID = Movies.MovieID



update Videos
set Videos.VideoStatusID = 1
from Videos, RentalRecord, Movies, Customers
where RentalRecord.VideoID = @VideoID and RentalRecord.MemberID = @MemberID and RentalRecord.VideoID = Videos.VideoID and Videos.MovieID = Movies.MovieID


end

else
begin
select 'No video id exists'
end


end


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-20 : 12:05:05
yup you need another if as datedue is from another table
Go to Top of Page

sas0riza
Yak Posting Veteran

56 Posts

Posted - 2008-11-20 : 12:11:08
quote:
Originally posted by visakh16

yup you need another if as datedue is from another table



ok thanks
Go to Top of Page

sas0riza
Yak Posting Veteran

56 Posts

Posted - 2008-11-20 : 14:28:36
Another one...thanks in advance!
I want to validate for duplicate VideoID insertion.

Like when I run sp_AddRentalRecord, I want it to check to see if that VideoID is rented already; if so, then print message and quit.

But in my SP, it's printing the message and also running the rest of the SP (which I don't want). I have a IF statement in there, but it doesn't seem to work.

RentalRecord
RentalID
MemberID
VideoID
DateDue

Videos
VideoID
MovieID
VideoStatusID

Movies
MovieID







create proc [dbo].[sp_AddRentalRecord]
--input parameters
(
@MemberID int,
@VideoID int
)
as
begin

if exists (select 1 from RentalRecord where VideoID = @VideoID and DateDue is null)
begin
select 'Video id is already rented'
return
end


begin transaction

declare @Total money
set @Total = (select Price*1.05 from Videos where VideoID = @VideoID)


insert into RentalRecord
values (@MemberID,@VideoID,ROUND(@Total,2),GETDATE(), DATEADD(dd,5,GETDATE()),null)

-- transaction error handling; if error number is not zero then there's a failure, rollback transaction, and print message
if @@error <> 0
begin
rollback transaction
select 'Insert Failed'
return
end

else
begin
select 'Rental Record Added Successfully'
end

commit transaction

end

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-21 : 01:59:47
put it in else

create proc [dbo].[sp_AddRentalRecord]
--input parameters
(
@MemberID int,
@VideoID int
)
as
begin

if exists (select 1 from RentalRecord where VideoID = @VideoID and DateDue is null)
begin
select 'Video id is already rented'
return
end

else
begin

begin transaction

declare @Total money
set @Total = (select Price*1.05 from Videos where VideoID = @VideoID)


insert into RentalRecord
values (@MemberID,@VideoID,ROUND(@Total,2),GETDATE(), DATEADD(dd,5,GETDATE()),null)

-- transaction error handling; if error number is not zero then there's a failure, rollback transaction, and print message
if @@error <> 0
begin
rollback transaction
select 'Insert Failed'
return
end

else
begin
select 'Rental Record Added Successfully'
end

commit transaction
end
end
Go to Top of Page

sas0riza
Yak Posting Veteran

56 Posts

Posted - 2008-11-21 : 10:13:21
quote:
Originally posted by visakh16

put it in else

create proc [dbo].[sp_AddRentalRecord]
--input parameters
(
@MemberID int,
@VideoID int
)
as
begin

if exists (select 1 from RentalRecord where VideoID = @VideoID and DateDue is null)
begin
select 'Video id is already rented'
return
end

else
begin

begin transaction

declare @Total money
set @Total = (select Price*1.05 from Videos where VideoID = @VideoID)


insert into RentalRecord
values (@MemberID,@VideoID,ROUND(@Total,2),GETDATE(), DATEADD(dd,5,GETDATE()),null)

-- transaction error handling; if error number is not zero then there's a failure, rollback transaction, and print message
if @@error <> 0
begin
rollback transaction
select 'Insert Failed'
return
end

else
begin
select 'Rental Record Added Successfully'
end

commit transaction
end
end




Oh that was easy enough. Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-21 : 10:16:23
Cheers
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-22 : 13:14:38
it should be like below
dont use variables to store values from inserted. inserted may not always contain a single row.

create trigger trg_i_customer
on Customers
for insert
as
begin



if exists(select 1 from inserted i
inner join Customers c
on c.Phone = i.Phone)
begin
if @@TRANCOUNT > 0
rollback transaction
Raiseerror 'Customer with phone number already exists',10,1


end

end
Go to Top of Page

sas0riza
Yak Posting Veteran

56 Posts

Posted - 2008-11-22 : 13:22:45
quote:
Originally posted by visakh16

it should be like below
dont use variables to store values from inserted. inserted may not always contain a single row.

create trigger trg_i_customer
on Customers
for insert
as
begin



if exists(select 1 from inserted i
inner join Customers c
on c.Phone = i.Phone)
begin
if @@TRANCOUNT > 0
rollback transaction
Raiseerror 'Customer with phone number already exists',10,1


end

end





Thank you!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-22 : 13:23:27
Welcome as always
Go to Top of Page

sas0riza
Yak Posting Veteran

56 Posts

Posted - 2008-11-22 : 13:24:32
quote:
Originally posted by visakh16

it should be like below
dont use variables to store values from inserted. inserted may not always contain a single row.

create trigger trg_i_customer
on Customers
for insert
as
begin



if exists(select 1 from inserted i
inner join Customers c
on c.Phone = i.Phone)
begin
if @@TRANCOUNT > 0
rollback transaction
Raiseerror 'Customer with phone number already exists',10,1


end

end




What's the difference between @@error and @@trancount? Thanks
Go to Top of Page
    Next Page

- Advertisement -