We help IT Professionals succeed at work.

Conversion failed when converting the nvarchar value 'One' to data type int.

johnkainn
johnkainn asked
on
580 Views
Last Modified: 2010-04-21
I have 4 tables
1) test_t1.  Columns: t1Id and wId.  
2) test_t2.  Columns: t2Id and wId.
3) test_w. Columns: wId and Word.   A table with words that the first two tables refer to.
4) test_P.  Columns: PId, t1Id, t2Id.
I created a stored procedures, that gets everything from test_P table and should also get Word from test_w table.  I tried to make a subquery, but I get following error:
Msg 245, Level 16, State 1, Procedure test_TestGet1, Line 12
Conversion failed when converting the nvarchar value 'One' to data type int.
ALTER PROCEDURE [dbo].[test_TestGet1]
	(
	@PId int,
	@t1Id int,
	@t2Id int
	)
AS
	SELECT * FROM test_P p
	JOIN test_t1 a
	ON p.t1Id=a.t1Id
	JOIN test_t2 b
	ON p.t2Id=b.t2Id
	WHERE @PId=1 AND @t1Id=(SELECT w.word FROM test_w w WHERE w.wId=a.wId AND a.t1Id=@t1Id) AND @t2Id=(SELECT w.word FROM test_w w WHERE w.wId=b.wId AND b.t2Id=@t2Id)

Open in new window

Comment
Watch Question

Is w.word an nvarchar type?  If so, that would cause the error.

Subselects are valid, but I don't think this query is doing what you want ... even if it would compile.

How about this JOIN solution?

ALTER PROCEDURE [dbo].[test_TestGet1]
        (
        @PId int,
        @t1Id int,
        @t2Id int
        )
AS
        SELECT p.*, a.*, b.*,
        W_A.word as WordA, W_B.word as WordB
        FROM test_P p
        JOIN test_t1 a
        ON p.t1Id=a.t1Id
        JOIN test_t2 b
        ON p.t2Id=b.t2Id
        Inner Join test_w W_A on W_A.wID = @t1ID
        Inner Join test_w W_B on W_B.wID = @t2ID
        WHERE @PId=1 

Open in new window

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
w.word is nvarchar type.  This inner Join doesn't work in my case. I need to match wId in test_t1 to wId in test_w.
I tried the cast but did not succeed. Maybe I did it wrong. Could you please change the subquery I sent and add the casting?
Thanks
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mark WillsTopic Advisor, Page Editor
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
The problem seems to be @t1Id=(SELECT w.word
because @t1Id is int and w.word is nvarchar, so think you want a.wid = (select w.wid
but you are not returning any values from test_w.

so, think what you might be looking for is something like :

ALTER PROCEDURE [dbo].[test_TestGet1]
      (
      @PId int,
      @t1Id int,
      @t2Id int
      )
AS
      SELECT * FROM test_P p
      JOIN test_t1 a ON p.t1Id=a.t1Id
      JOIN test_w aw ON a.wId=aw.wId
      JOIN test_t2 b ON p.t2Id=b.t2Id
      JOIN test_w bw ON b.wId=bw.wId

Author

Commented:
I went over my code and the only parameter I need is PId. So following is my revised code with the casting. If I execute I do not get anything returned. For PId=1, I should get returned
PId=1, T1Id=1, Word(t1)="Two", T2Id=2, Word(t2)="One"
Here is what is in my tables.
test_P (row1) : PId=1, t1Id=1, t2Id=1
test_t1(row1): t1Id=1, wId=2
test_t2(row1):t2Id=1, wId=1
test_w:(row1): wId=1, word="One"
             (row2):wId=2,word="Two"



CREATE PROCEDURE [dbo].[test_TestGet3]
	(
	@PId int
	)
AS
	SELECT * FROM test_P p
	JOIN test_t1 a
	ON p.t1Id=a.t1Id
	JOIN test_t2 b
	ON p.t2Id=b.t2Id
	WHERE @PId=p.PId AND cast(p.t1Id as nvarchar(100))=(SELECT w.word FROM test_w w WHERE w.wId=a.wId) AND cast(p.t2Id as nvarchar(100))=(SELECT w.word FROM test_w w WHERE w.wId=b.wId)

Open in new window

Mark WillsTopic Advisor, Page Editor
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
have you looked at my code yet ?

ALTER PROCEDURE [dbo].[test_TestGet1]
      (
      @PId int,
      @t1Id int,
      @t2Id int
      )
AS
      SELECT * FROM test_P p
      JOIN test_t1 a ON p.t1Id=a.t1Id
      JOIN test_w aw ON a.wId=aw.wId
      JOIN test_t2 b ON p.t2Id=b.t2Id
      JOIN test_w bw ON b.wId=bw.wId

Topic Advisor, Page Editor
CERTIFIED EXPERT
Distinguished Expert 2018
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you all very much for your help.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.