sqlserver數據庫創建視圖時如何改變字段類型以及查詢視圖詳細內容

創建或修改視圖:

create(alter)  view abc as

select

           l.id as lId,

           cast(l.password as int) as password,                       字符串 ---> int

           cast(l.age as int) as ageInt,                                      字符串 ---> int

           convert(nvarchar(255),p.id) as idString,                   int --->  字符串

           convert(nvarchar(255),p.weight) as weightString,    int --->  字符串

           convert (nvarchar(255) , p.price) as priceString       int --->  字符串

from dbo.login as l , dbo.product as p

;


重點關注cast 和 convert 關鍵字的用法。


sp_help 視圖名字;查詢視圖詳細內容

----------------------------------------------------------------------------------------------------------

下方這些爲轉載信息:

1、ALTER viewtest1 as 
select NAME ASNAMEINT,AGE AS AGEINT FROM TEST (name保持表中類型)

2、sp_help test1

NAMEINT    varchar     no 50        yes no yesChinese_PRC_CI_AS
AGEINT        int            no 4 10   0    yes (n/a) (n/a) NULL

1、ALTER view test1 as 
select CAST(NAME AS INT) AS NAMEINT,AGE AS AGEINT FROMTEST  (將varchar類型改爲int類型)

2、sp_help test1

NAMEINT   int no 4 10   0    yes (n/a) (n/a) NULL
AGEINT       int no 4 10   0    yes (n/a) (n/a) NULL

----------------------------------------------------------------------------------------------------------

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章