What does N' stands for in a SQL script ? (the one used before characters in insert script)

后端 未结 7 2396
孤城傲影
孤城傲影 2020-11-29 09:05

I generated an sql script like this,

INSERT [dbo].[TableName] ([Sno], [Name], [EmployeeId], [ProjectId], [Experience]) 
VALUES (1, N\'Dave\', N\'ESD157\', N\         


        
相关标签:
7条回答
  • 2020-11-29 09:32

    'abcd' is a literal for a [var]char string (or maybe text, but varchar(max) would be more common now) - occupying 4 bytes memory, and using whatever code-page the SQL server is configured for. N'abcd' is a literal for a n[var]char string (or maybe ntext, but nvarchar(max) would be preferable), occupying 8 bytes of memory using UTF-16. This allows for full international usage, and frankly n[var]char should probably be the default in most systems.

    0 讨论(0)
  • 2020-11-29 09:33

    N is used to specify a unicode string.

    Here's a good discussion: Why do some SQL strings have an 'N' prefix?

    In your example N prefix is not required because ASCII characters (with value less than 128) map directly to unicode. However, if you wanted to insert a name that was not ASCII then the N prefix would be required.

    INSERT [dbo].[TableName] ([Sno], [Name], [EmployeeId], [ProjectId], [Experience]) 
    VALUES (1, N'Wāhi', 'ESD157', 'FD080', 7)
    
    0 讨论(0)
  • 2020-11-29 09:36
    DECLARE
    
       TYPE name_salary_rt IS RECORD (
    
          table_names     VARCHAR2 (1000),
    
          counts  NUMBER
    
       );
    
    VSQL varchar2(2000);
    
       TYPE name_salary_aat IS TABLE OF name_salary_rt
    
          INDEX BY PLS_INTEGER;
    
    
       l_employees   name_salary_aat;
    
    BEGIN
    
       EXECUTE IMMEDIATE
    
          q'[select table_name ,count(*) CountF
    
               from all_tab_columns  where rownum<100
    
             group by table_name]'
    
          BULK COLLECT INTO l_employees;
    
    
       FOR indx IN 1 .. l_employees.COUNT
    
       LOOP
    
    VSQL:=VSQL||'  select '''||l_employees (indx).table_names||''','''|| l_employees (indx).counts ||''' from dual ';
    
    if indx<l_employees.COUNT then
    
    VSQL:=VSQL|| ' union all ';
    
    else
    
    VSQL:=VSQL||';';
    
    end if;
    
    
      --    DBMS_OUTPUT.put_line (l_employees (indx).table_names||','|| l_employees (indx).counts);
    
        --    DBMS_OUTPUT.put_line (l_employees (indx).countf);
    
       END LOOP;
    
    DBMS_OUTPUT.put_line (VSQL);
    
    -- execute immediate  VSQL; 
    
    END;
    
    
    0 讨论(0)
  • 2020-11-29 09:44

    each country has its own specific letters and symbols so a database set up for English US will not recognise the £ symbol which a English UK database would, the same goes for Spanish, French, German

    Also other languages like Chinese, Japanese, Hebrew, Arabic don't use any Latin characters.

    so anyone trying to enter any data not contained in the local character set will fail or suffer data corruption, if you are using varchar, so if there is even the remotest possibility that your database will need to support more than one local character set then you have to use the nationalised language character set aka unicode aka NChar, which allows the character sets nationality to be recorded with the character. providing international text support

    Likewise adding the N Prefix to a string instructs the database to include the Nation code as well as the character code

    0 讨论(0)
  • 2020-11-29 09:46

    This denotes that the subsequent string is in Unicode (the N actually stands for National language character set).

    Which means that you are passing an NCHAR, NVARCHAR or NTEXT value, as opposed to CHAR, VARCHAR or TEXT.

    • SOURCE
    0 讨论(0)
  • 2020-11-29 09:51

    N is to specify that its a string type value.

    [N]'tsql_string'

    Is a constant string. tsql_string can be any nvarchar or varchar data type. If the N is included, the string is interpreted as nvarchar data type.

    0 讨论(0)
提交回复
热议问题