다음과 같은 문제가 있습니다. 하나의 열에 텍스트와 숫자가 포함 된 SQL 테이블이 있습니다. 텍스트와 숫자를 처리하기 위해 두 가지보기를 만들었습니다. 나는 전체의 뷰를 선택하면 예상대로T-SQL 뷰 문 순서 순서
, 그들은 작동하지만 내가 선택 문에 어디 문을 추가 할 경우, 나는 다음과 같은 오류 얻을 :
Conversion failed when converting the varchar value 'Thomas' to data type int.
는 어떻게 든 서버를 말할 수를 내부 뷰의 where 문을 외부 선택 전에 적용할까요?
오류를 재생 다음 SQL 코드를 사용하십시오 :
create schema tst
go
create table tst.tbl(strValue varchar(10))
insert tst.tbl(strValue)
values ('Thomas'), ('1991'), ('Reto'), ('21'), ('Strub')
go
create view tst.tblStr as
select strValue
from tst.tbl
where isnumeric(strValue)=0
go
create view tst.tblInt as
select cast(strValue as int) intValue
from tst.tbl
where isnumeric(strValue)=1
go
select * from tst.tblStr order by strValue --ok
select * from tst.tblInt order by intValue --ok
go
select * from tst.tblInt where intValue<100 --not ok
go
select * from tst.tbl where isnumeric(strValue)=1 and cast(strValue as int) < 100 --ok
go
drop view tst.tblInt
drop view tst.tblStr
drop table tst.tbl
drop schema tst
귀하의 오류 메시지를 확인하십시오 한 줄을 modfied 제안 (데이터 유형 int.'에 VARCHAR 값 '토마스'를 변환 할 때 '변환 실패) 'Thomas'는'tst.tblInt'에 있습니다. 어떻게 그런 일이 일어 났습니까? – Kaf
그게 아니야 ... 그냥 방금 잘못된 순서로 조항을 적용 ... – StrubT