denisenkom / go-mssqldb

Microsoft SQL server driver written in go language
BSD 3-Clause "New" or "Revised" License
1.82k stars 499 forks source link

Rows.Scan scans as null for non null strings #373

Open ghost opened 6 years ago

ghost commented 6 years ago

I have tried both string and sql.NullString for scanning a NVARCHAR column, but it reads null even the row has non null value.

jeremejazz commented 6 years ago

Happens to me as well.. Especially for NVARCHAR columns

kardianos commented 6 years ago

@anandbose or @jeremejazz Can either one of you post a reproducible example?

jeremejazz commented 6 years ago

I'll see if I can

Here is the SQL Table definition Collation: SQL_Latin1_General_CP1_CI_AS

COLUMN_NAME DATA_TYPE TYPE_NAME PRECISION LENGTH SCALE RADIX SQL_DATA_TYPE CHAR_OCTET_LENGTH SS_DATA_TYPE
OBJECTID 6 float 15 8 NULL 10 6 NULL 109
Status -9 nvarchar 255 510 NULL NULL -9 510 39
Addr_type -9 nvarchar 255 510 NULL NULL -9 510 39
X 6 float 15 8 NULL 10 6 NULL 109
Y 6 float 15 8 NULL 10 6 NULL 109
PMT_ID 4 int 10 4 0 10 4 NULL 56
fcs_id 6 float 15 8 NULL 10 6 NULL 109
epa_id 12 varchar 1000 1000 NULL NULL 12 1000 39
name -9 nvarchar 4000 8000 NULL NULL -9 8000 39
addr1 -9 nvarchar 4000 8000 NULL NULL -9 8000 39
addr2 -9 nvarchar 4000 8000 NULL NULL -9 8000 39
city -9 nvarchar 255 510 NULL NULL -9 510 39
county -9 nvarchar 255 510 NULL NULL -9 510 39
state -9 nvarchar 255 510 NULL NULL -9 510 39
country -9 nvarchar 255 510 NULL NULL -9 510 39

using the following code :

type MSSQLResult struct {
    Status               string
    X                    float32
    Y                    float32
    Pmtid                int
    Fcsid                int
    Epaid                int
    Propname             string
    Propaddr1            string
    Propcity             string
    Propcounty           string
    Propstate            string
    Propcountry          string
}

...

query := "select status, x, y, pmt_id, fcs_id, epa_id, name, addr1, city, county, state, country "
query += " from mytable order by pmt_id"
query += " offset " + strconv.Itoa(offset) + " rows fetch next " + strconv.Itoa(limit) + " rows only"

stmt, err := m.conn.Prepare(query)

if err != nil {
    panic(err)
}

for results.Next() {
        var status string
        var x,
            y float32
        var pmtid,
            fcsid,
            epaid int

        var propname,
            propaddr1,
            propcity,
            propcounty,
            propstate,
            propcountry string

    results.Scan(&status, &x, &y, &pmtid, &fcsid, &epaid, &propname, &propaddr1, &propcity, &propcounty, &propstate, &propcountry)
    fmt.Println("Test Output:", status, x, y, pmtid, fcsid, epaid, propname, propaddr1, propcity, propcounty, propstate, propcountry)

}

From here the output I am getting is Test Output: M -87.537445 33.208057 40 3280 0 Test Output: M -85.96762 33.62325 109 3349 0 Test Output: M -87.49709 33.243145 498 3738 0

Which is expected to Print the name, address, city, county, state, country fields The status field is Printed though as 'M' along with x, y, pmt_id, and epa_id the has no value even though there is in the database based on the pmt_id. I have even tried casting the nvarchar(4000) fields to varchar(255) but still no avail. I cannot determine if this has something to do with the character though most of them are using plain alphabet.

I have tried querying the same table on python and I am getting a results for the non null fields that displayed null in go-mssqldb.

I hope this helps.

jeremejazz commented 6 years ago

Above code was using sqlserver connection string. I have also tried odbc connection but the output produced is still the same

denisenkom commented 6 years ago

Can you enable verbose logging and provide log output?