Search code examples
sql-serverssms

SQL hyperlink - SSMS comes back with multiple errors


I am new to SQL and I am teaching myself at home. I have been following a tutorial at home and, at the same time, I am trying to get creative. I am trying to link a hyperlink and I am keep getting an error.

Below is the code I found on a previous comment on Stack Overflow:

Medicalrecord Select CAST('https://www.google.com' as XML)

The idea is to create a chart with a column called "Medical records" and have a hyperlinked pdf to it.

Please find the full code below:

Create Table MedicalFiles
(
    PatientName varchar(50),
    DoctorName varchar(50),
    DoctorSpecialy varchar(50),
    reasonforthevisit varchar(50),
    doctorvisitcost int,
    medicationcost int,
    additionalcostsdescription varchar(50),
    Additionalcostvalue int,
    Medicalrecord Select CAST('https://www.google.com' as XML)
    )

Here are the errors I'm getting:

Msg 173, Level 15, State 13, Line 11 The definition for column 'Medicalrecord' must include a data type.
Msg 102, Level 15, State 1, Line 12 Incorrect syntax near ')'.


Solution

  • CREATE TABLE syntax does not allow a SELECT query for a computed column definition. The DDL below will create a computed column with the XML constant so it can be used in SELECT queries:

    CREATE TABLE dbo.MedicalFiles
    (
        PatientName varchar(50),
        DoctorName varchar(50),
        DoctorSpecialy varchar(50),
        reasonforthevisit varchar(50),
        doctorvisitcost int,
        medicationcost int,
        additionalcostsdescription varchar(50),
        Additionalcostvalue int,
        Medicalrecord AS CAST('https://www.google.com' as XML)
    );