在SQL Server视图中更改数据类型。

huangapple go评论89阅读模式
英文:

Change data type in a SQL Server view

问题

我有一个名为dbo.NpsResultsView的视图。

这个视图的代码如下:

SELECT        
    dbo.CRMDeals.dealid,
    dbo.CRMDeals.contact,
    dbo.CRMContacts.title,
    dbo.CRMContacts.firstname,
    dbo.CRMContacts.lastname, 
    dbo.CRMContacts.dealCustomFieldId,
    CAST(dbo.CRMCustomFieldsData.fieldValue AS INT) AS fieldValue,
    dbo.CRMCustomFieldsData.updatedTimestamp, 
    dbo.ClientsMigrationDataset.Groups AS Expr1, 
    dbo.ClientsMigrationDataset.Client,
    dbo.ClientsMigrationDataset.Email,
    dbo.ClientsMigrationDataset.ClientManager
FROM
    dbo.CRMDeals
INNER JOIN    
    dbo.CRMContacts ON dbo.CRMContacts.contactid = dbo.CRMDeals.contact 
INNER JOIN    
    dbo.CRMDealCustomFieldsData ON dbo.CRMDealCustomFieldsData.dealId = dbo.CRMDeals.dealid 
INNER JOIN 
    dbo.ClientsMigrationDataset ON dbo.ClientsMigrationDataset.Email = dbo.CRMContacts.email
WHERE        
    (dbo.CRMDealCustomFieldsData.dealCustomFieldId = '15')

在这个视图中,我有一个名为fieldValue的列,它被返回为VarChar,但它需要是int,因为它是一个10分制的分数。

注意:使用SQL Server Management Studio。

英文:

I have a view called dbo.NpsResultsView.

The code for this view is:

SELECT        
    dbo.CRMDeals.dealid,
    dbo.CRMDeals.contact,
    dbo.CRMContacts.title,
    dbo.CRMContacts.firstname,
    dbo.CRMContacts.lastname, 
    dbo.CRMContacts.dealCustomFieldId,
    dbo.CRMCustomFieldsData.fieldValue,
    dbo.CRMCustomFieldsData.updatedTimestamp, 
    dbo.ClientsMigrationDataset.Groups AS Expr1, 
    dbo.ClientsMigrationDataset.Client,
    dbo.ClientsMigrationDataset.Email,
    dbo.ClientsMigrationDataset.ClientManager
FROM
    dbo.CRMDeals
INNER JOIN    
    dbo.CRMContacts ON dbo.CRMContacts.contactid = dbo.CRMDeals.contact 
INNER JOIN    
    dbo.CRMDealCustomFieldsData ON dbo.CRMDealCustomFieldsData.dealId = dbo.CRMDeals.dealid 
INNER JOIN 
    dbo.ClientsMigrationDataset ON dbo.ClientsMigrationDataset.Email = dbo.CRMContacts.email
WHERE        
    (dbo.CRMDealCustomFieldsData.dealCustomFieldId = '15')

In that view, I have a column called fieldValue which is returned as VarChar, and this needs to be int as it's a score out of 10.

Note: using SQL Server Management Studio.

答案1

得分: 2

使用CAST(fieldValue AS INT)在你的视图定义中 - 或者如果你希望在fieldValue中的值不是真正的INT时避免异常,也可以使用TRY_CAST(fieldValue AS INT)。然后,任何“不合适”的值都将被替换为NULL,而不是引发异常。

尝试这样做:

CREATE VIEW dbo.NpsResultsView
AS
    SELECT        
        dbo.CRMDeals.dealid,
        dbo.CRMDeals.contact,
        dbo.CRMContacts.title,
        dbo.CRMContacts.firstname,
        dbo.CRMContacts.lastname, 
        dbo.CRMContacts.dealCustomFieldId,
        TRY_CAST(dbo.CRMCustomFieldsData.fieldValue AS INT) AS 'fieldValueInt',
        dbo.CRMCustomFieldsData.updatedTimestamp, 
        dbo.ClientsMigrationDataset.Groups AS Expr1, 
        dbo.ClientsMigrationDataset.Client,
        dbo.ClientsMigrationDataset.Email,
        dbo.ClientsMigrationDataset.ClientManager
    FROM
        dbo.CRMDeals
    INNER JOIN    
        dbo.CRMContacts ON dbo.CRMContacts.contactid = dbo.CRMDeals.contact 
    INNER JOIN    
        dbo.CRMDealCustomFieldsData ON dbo.CRMDealCustomFieldsData.dealId = dbo.CRMDeals.dealid 
    INNER JOIN 
        dbo.ClientsMigrationDataset ON dbo.ClientsMigrationDataset.Email = dbo.CRMContacts.email
    WHERE         
        (dbo.CRMDealCustomFieldsData.dealCustomFieldId = '15')
英文:

Use CAST(fieldValue AS INT) in your view definition - or if you want to avoid an exception when a value in fieldValue isn't really an INT, maybe use TRY_CAST(fieldValue AS INT). Then any "improper" value will be replaced by NULL instead of throwing an exception.

Try this:

CREATE VIEW dbo.NpsResultsView
AS
    SELECT        
        dbo.CRMDeals.dealid,
        dbo.CRMDeals.contact,
        dbo.CRMContacts.title,
        dbo.CRMContacts.firstname,
        dbo.CRMContacts.lastname, 
        dbo.CRMContacts.dealCustomFieldId,
        TRY_CAST(dbo.CRMCustomFieldsData.fieldValue AS INT) AS 'fieldValueInt',
        dbo.CRMCustomFieldsData.updatedTimestamp, 
        dbo.ClientsMigrationDataset.Groups AS Expr1, 
        dbo.ClientsMigrationDataset.Client,
        dbo.ClientsMigrationDataset.Email,
        dbo.ClientsMigrationDataset.ClientManager
    FROM
        dbo.CRMDeals
    INNER JOIN    
        dbo.CRMContacts ON dbo.CRMContacts.contactid = dbo.CRMDeals.contact 
    INNER JOIN    
        dbo.CRMDealCustomFieldsData ON dbo.CRMDealCustomFieldsData.dealId = dbo.CRMDeals.dealid 
    INNER JOIN 
        dbo.ClientsMigrationDataset ON dbo.ClientsMigrationDataset.Email = dbo.CRMContacts.email
    WHERE         
        (dbo.CRMDealCustomFieldsData.dealCustomFieldId = '15')

答案2

得分: 0

请注意,您不能使用此命令更改视图的定义。要更改视图定义,您必须删除视图,然后重新创建它。

CREATE VIEW AView
AS
SELECT CAST(title AS char(50))
FROM titles

您可以使用 CAST 和 CONVERT 函数来更改数据类型。

英文:

Note that you cannot use this command to change the definition for a view. To change the view definition, you must drop the view and then recreate it.

CREATE VIEW AView
AS
SELECT CAST(title AS char(50))
FROM titles

You can use cast and covert function to change a datatype

huangapple
  • 本文由 发表于 2023年7月10日 13:55:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定