如何从SQL server DB中检索值?

时间:2021-12-03 01:40:14

The following query works fine:

以下查询工作正常:

select ProductNumber,Name,Color,ListPrice 
from Production.Product 
where ProductNumber = 'AR-5381'

Where as when, I'm writing my code like following to retrieve value from SQL sever:

当我在写代码时,像下面这样,从SQL sever中检索值:

declare @ProductNum nvarchar
set @ProductNum = 'AR-5381'
select ProductNumber,Name,Color,ListPrice 
from Production.Product 
where ProductNumber = @ProductNum 

I'm not able to get the values. But ProductNumber('AR-5381') is present in the table. What I'm doing wrong?

我无法得到这些值。但是ProductNumber(“AR-5381”)在表格中。我做错了什么吗?

1 个解决方案

#1


4  

Variable is declared with default length which is 1.

变量声明为默认长度为1。

nvarchar without specifying length means nvarchar(1)

不指定长度的nvarchar表示nvarchar(1)

so following nvarchar varaible only store first character in the string because its length is one.

所以跟随nvarchar varaible只在字符串中存储第一个字符,因为它的长度是1。

declare @ProductNum nvarchar
set @ProductNum = 'AR-5381'
SELECT @ProductNum 

Output
A

输出一个

declare @ProductNum nvarchar(2)
set @ProductNum = 'AR-5381'
SELECT @ProductNum 

Output
AR

输出基于“增大化现实”技术

You should specify some length like.

您应该指定一些长度,比如。

declare @ProductNum nvarchar(255)
set @ProductNum = 'AR-5381'
select ProductNumber,Name,Color,ListPrice 
from Production.Product 
where ProductNumber =@ProductNum 

#1


4  

Variable is declared with default length which is 1.

变量声明为默认长度为1。

nvarchar without specifying length means nvarchar(1)

不指定长度的nvarchar表示nvarchar(1)

so following nvarchar varaible only store first character in the string because its length is one.

所以跟随nvarchar varaible只在字符串中存储第一个字符,因为它的长度是1。

declare @ProductNum nvarchar
set @ProductNum = 'AR-5381'
SELECT @ProductNum 

Output
A

输出一个

declare @ProductNum nvarchar(2)
set @ProductNum = 'AR-5381'
SELECT @ProductNum 

Output
AR

输出基于“增大化现实”技术

You should specify some length like.

您应该指定一些长度,比如。

declare @ProductNum nvarchar(255)
set @ProductNum = 'AR-5381'
select ProductNumber,Name,Color,ListPrice 
from Production.Product 
where ProductNumber =@ProductNum