Sql server SQL CLR C#UDF-将国家代码附加到邮政编码

Sql server SQL CLR C#UDF-将国家代码附加到邮政编码,sql-server,user-defined-functions,sqlclr,Sql Server,User Defined Functions,Sqlclr,我有以下SQL CLR C#UDF: 这基本上接受两个参数,一个国家名称和一个邮政编码,然后返回带有国家代码前缀的邮政编码。非常基本的东西,但是我对它的性能不满意 我听说将这个国家和代码列表添加为资源,然后填充一个列表可能会有所帮助,但我不知道如何做到这一点 有什么想法吗 using System; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServ

我有以下SQL CLR C#UDF:

这基本上接受两个参数,一个国家名称和一个邮政编码,然后返回带有国家代码前缀的邮政编码。非常基本的东西,但是我对它的性能不满意

我听说将这个国家和代码列表添加为资源,然后填充一个列表可能会有所帮助,但我不知道如何做到这一点

有什么想法吗

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public partial class UserDefinedFunctions
{
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlString clrFn_AddCountryCode(string theCountry, string thePostcode)
    {

        if (thePostcode.Length == 0 || String.IsNullOrEmpty(thePostcode)) { return (SqlString)(""); }

        else if (theCountry == "FRENCH POLYNESIA") { return new SqlString("183" + thePostcode); }
        else if (theCountry == "GUADELOUPE") { return new SqlString("181" + thePostcode); }
        else if (theCountry == "ALAND") { return new SqlString("360" + thePostcode); }
        else if (theCountry == "AFGHANISTAN") { return new SqlString("100" + thePostcode); }
        else if (theCountry == "ALBANIA") { return new SqlString("101" + thePostcode); }
        else if (theCountry == "ALGERIA") { return new SqlString("102" + thePostcode); }
        else if (theCountry == "AMERICAN SAMOA") { return new SqlString("103" + thePostcode); }
        else if (theCountry == "ANDORRA") { return new SqlString("104" + thePostcode); }
        else if (theCountry == "ANGOLA") { return new SqlString("105" + thePostcode); }
        else if (theCountry == "ANGUILLA") { return new SqlString("106" + thePostcode); }
        else if (theCountry == "ANTIGUA & BARBUDA") { return new SqlString("107" + thePostcode); }
        else if (theCountry == "ARGENTINA") { return new SqlString("108" + thePostcode); }
        else if (theCountry == "ARMENIA") { return new SqlString("109" + thePostcode); }
        else if (theCountry == "ARUBA") { return new SqlString("110" + thePostcode); }
        else if (theCountry == "ASCENSION ISLAND") { return new SqlString("111" + thePostcode); }
        else if (theCountry == "AUSTRALIA") { return new SqlString("112" + thePostcode); }
        else if (theCountry == "AUSTRIA") { return new SqlString("113" + thePostcode); }
        else if (theCountry == "AZERBAIJAN") { return new SqlString("114" + thePostcode); }
        else if (theCountry == "BAHAMAS") { return new SqlString("115" + thePostcode); }
        else if (theCountry == "BAHRAIN") { return new SqlString("116" + thePostcode); }
        else if (theCountry == "BANGLADESH") { return new SqlString("117" + thePostcode); }
        else if (theCountry == "BARBADOS") { return new SqlString("118" + thePostcode); }
        else if (theCountry == "BELARUS") { return new SqlString("119" + thePostcode); }
        else if (theCountry == "BELGIUM") { return new SqlString("120" + thePostcode); }
        else if (theCountry == "BELIZE") { return new SqlString("121" + thePostcode); }
        else if (theCountry == "BENIN") { return new SqlString("122" + thePostcode); }
        else if (theCountry == "BERMUDA") { return new SqlString("123" + thePostcode); }
        else if (theCountry == "BHUTAN") { return new SqlString("124" + thePostcode); }
        else if (theCountry == "BOLIVIA") { return new SqlString("125" + thePostcode); }
        else if (theCountry == "BOSNIA & HERZEGOVINA") { return new SqlString("128" + thePostcode); }
        else if (theCountry == "BOTSWANA") { return new SqlString("129" + thePostcode); }
        else if (theCountry == "BRAZIL") { return new SqlString("130" + thePostcode); }
        else if (theCountry == "BRITISH INDIAN OCEAN TERRITORY") { return new SqlString("131" + thePostcode); }
        else if (theCountry == "BRITISH VIRGIN ISLANDS") { return new SqlString("132" + thePostcode); }
        else if (theCountry == "BRUNEI DARUSSALAM") { return new SqlString("134" + thePostcode); }
        else if (theCountry == "BULGARIA") { return new SqlString("135" + thePostcode); }
        else if (theCountry == "BURKINA FASO") { return new SqlString("136" + thePostcode); }
        else if (theCountry == "BURUNDI") { return new SqlString("138" + thePostcode); }
        else if (theCountry == "CAMBODIA") { return new SqlString("139" + thePostcode); }
        else if (theCountry == "CAMEROON") { return new SqlString("140" + thePostcode); }
        else if (theCountry == "CANADA") { return new SqlString("141" + thePostcode); }
        else if (theCountry == "CAPE VERDE") { return new SqlString("142" + thePostcode); }
        else if (theCountry == "CAYMAN ISLANDS") { return new SqlString("144" + thePostcode); }
        else if (theCountry == "CENTRAL AFRICAN REPUBLIC") { return new SqlString("145" + thePostcode); }
        else if (theCountry == "CHAD") { return new SqlString("146" + thePostcode); }
        else if (theCountry == "CHILE") { return new SqlString("147" + thePostcode); }
        else if (theCountry == "CHINA") { return new SqlString("148" + thePostcode); }
        else if (theCountry == "COLOMBIA") { return new SqlString("151" + thePostcode); }
        else if (theCountry == "COMOROS") { return new SqlString("152" + thePostcode); }
        else if (theCountry == "COOK ISLANDS") { return new SqlString("154" + thePostcode); }
        else if (theCountry == "COSTA RICA") { return new SqlString("155" + thePostcode); }
        else if (theCountry == "CÔTE D'IVOIRE") { return new SqlString("213" + thePostcode); }
        else if (theCountry == "CROATIA") { return new SqlString("157" + thePostcode); }
        else if (theCountry == "CUBA") { return new SqlString("158" + thePostcode); }
        else if (theCountry == "CYPRUS") { return new SqlString("160" + thePostcode); }
        else if (theCountry == "CZECH REPUBLIC") { return new SqlString("161" + thePostcode); }
        else if (theCountry == "DEMOCRATIC REPUBLIC OF CONGO") { return new SqlString("351" + thePostcode); }
        else if (theCountry == "DENMARK") { return new SqlString("162" + thePostcode); }
        else if (theCountry == "DJIBOUTI") { return new SqlString("163" + thePostcode); }
        else if (theCountry == "DOMINICA") { return new SqlString("164" + thePostcode); }
        else if (theCountry == "DOMINICAN REPUBLIC") { return new SqlString("165" + thePostcode); }
        else if (theCountry == "ECUADOR") { return new SqlString("168" + thePostcode); }
        else if (theCountry == "EGYPT") { return new SqlString("169" + thePostcode); }
        else if (theCountry == "EL SALVADOR") { return new SqlString("171" + thePostcode); }
        else if (theCountry == "EQUATORIAL GUINEA") { return new SqlString("172" + thePostcode); }
        else if (theCountry == "ERITREA") { return new SqlString("173" + thePostcode); }
        else if (theCountry == "ESTONIA") { return new SqlString("174" + thePostcode); }
        else if (theCountry == "ETHIOPIA") { return new SqlString("175" + thePostcode); }
        else if (theCountry == "FALKLAND ISLANDS") { return new SqlString("177" + thePostcode); }
        else if (theCountry == "FAROE ISLANDS") { return new SqlString("176" + thePostcode); }
        else if (theCountry == "FIJI") { return new SqlString("178" + thePostcode); }
        else if (theCountry == "FINLAND") { return new SqlString("179" + thePostcode); }
        else if (theCountry == "FRANCE") { return new SqlString("180" + thePostcode); }
        else if (theCountry == "FRENCH GUIANA") { return new SqlString("182" + thePostcode); }
        else if (theCountry == "GABON") { return new SqlString("186" + thePostcode); }
        else if (theCountry == "GAMBIA") { return new SqlString("187" + thePostcode); }
        else if (theCountry == "GEORGIA") { return new SqlString("283" + thePostcode); }
        else if (theCountry == "GERMANY") { return new SqlString("189" + thePostcode); }
        else if (theCountry == "GHANA") { return new SqlString("190" + thePostcode); }
        else if (theCountry == "GIBRALTAR") { return new SqlString("191" + thePostcode); }
        else if (theCountry == "GREECE") { return new SqlString("192" + thePostcode); }
        else if (theCountry == "GREENLAND") { return new SqlString("193" + thePostcode); }
        else if (theCountry == "GRENADA") { return new SqlString("194" + thePostcode); }
        else if (theCountry == "GUADELOUPE") { return new SqlString("195" + thePostcode); }
        else if (theCountry == "GUAM") { return new SqlString("196" + thePostcode); }
        else if (theCountry == "GUATEMALA") { return new SqlString("197" + thePostcode); }
        else if (theCountry == "GUINEA") { return new SqlString("198" + thePostcode); }
        else if (theCountry == "GUINEA BISSAU") { return new SqlString("199" + thePostcode); }
        else if (theCountry == "GUYANA") { return new SqlString("200" + thePostcode); }
        else if (theCountry == "HAITI") { return new SqlString("201" + thePostcode); }
        else if (theCountry == "HONDURAS") { return new SqlString("203" + thePostcode); }
        else if (theCountry == "HONG KONG") { return new SqlString("204" + thePostcode); }
        else if (theCountry == "HUNGARY") { return new SqlString("205" + thePostcode); }
        else if (theCountry == "ICELAND") { return new SqlString("206" + thePostcode); }
        else if (theCountry == "INDIA") { return new SqlString("207" + thePostcode); }
        else if (theCountry == "INDONESIA") { return new SqlString("208" + thePostcode); }
        else if (theCountry == "IRAN") { return new SqlString("209" + thePostcode); }
        else if (theCountry == "IRAQ") { return new SqlString("210" + thePostcode); }
        else if (theCountry == "IRELAND") { return new SqlString("170" + thePostcode); }
        else if (theCountry == "ISRAEL") { return new SqlString("211" + thePostcode); }
        else if (theCountry == "ITALY") { return new SqlString("212" + thePostcode); }
        else if (theCountry == "JAMAICA") { return new SqlString("214" + thePostcode); }
        else if (theCountry == "JAPAN") { return new SqlString("215" + thePostcode); }
        else if (theCountry == "JORDAN") { return new SqlString("216" + thePostcode); }
        else if (theCountry == "KAZAKHSTAN") { return new SqlString("217" + thePostcode); }
        else if (theCountry == "KENYA") { return new SqlString("218" + thePostcode); }
        else if (theCountry == "KIRIBATI") { return new SqlString("219" + thePostcode); }
        else if (theCountry == "KOSOVO (UNMIK)") { return new SqlString("358" + thePostcode); }
        else if (theCountry == "KUWAIT") { return new SqlString("220" + thePostcode); }
        else if (theCountry == "KYRGYZSTAN") { return new SqlString("221" + thePostcode); }
        else if (theCountry == "LAOS") { return new SqlString("222" + thePostcode); }
        else if (theCountry == "LATVIA") { return new SqlString("223" + thePostcode); }
        else if (theCountry == "LEBANON") { return new SqlString("224" + thePostcode); }
        else if (theCountry == "LESOTHO") { return new SqlString("225" + thePostcode); }
        else if (theCountry == "LIBERIA") { return new SqlString("226" + thePostcode); }
        else if (theCountry == "LIBYA") { return new SqlString("227" + thePostcode); }
        else if (theCountry == "LIECHTENSTEIN") { return new SqlString("228" + thePostcode); }
        else if (theCountry == "LITHUANIA") { return new SqlString("229" + thePostcode); }
        else if (theCountry == "LUXEMBOURG") { return new SqlString("230" + thePostcode); }
        else if (theCountry == "MACAU") { return new SqlString("231" + thePostcode); }
        else if (theCountry == "MACEDONIA") { return new SqlString("232" + thePostcode); }
        else if (theCountry == "MADAGASCAR") { return new SqlString("233" + thePostcode); }
        else if (theCountry == "MALAWI") { return new SqlString("234" + thePostcode); }
        else if (theCountry == "MALAYSIA") { return new SqlString("235" + thePostcode); }
        else if (theCountry == "MALDIVES") { return new SqlString("236" + thePostcode); }
        else if (theCountry == "MALI") { return new SqlString("237" + thePostcode); }
        else if (theCountry == "MALTA") { return new SqlString("238" + thePostcode); }
        else if (theCountry == "MARSHALL ISLANDS") { return new SqlString("240" + thePostcode); }
        else if (theCountry == "MARTINIQUE") { return new SqlString("241" + thePostcode); }
        else if (theCountry == "MAURITANIA") { return new SqlString("242" + thePostcode); }
        else if (theCountry == "MAURITIUS") { return new SqlString("243" + thePostcode); }
        else if (theCountry == "MAYOTTE") { return new SqlString("244" + thePostcode); }
        else if (theCountry == "MEXICO") { return new SqlString("245" + thePostcode); }
        else if (theCountry == "MICRONESIA") { return new SqlString("246" + thePostcode); }
        else if (theCountry == "MOLDOVA") { return new SqlString("247" + thePostcode); }
        else if (theCountry == "MONACO") { return new SqlString("248" + thePostcode); }
        else if (theCountry == "MONGOLIA") { return new SqlString("249" + thePostcode); }
        else if (theCountry == "MONTENEGRO") { return new SqlString("357" + thePostcode); }
        else if (theCountry == "MONTSERRAT") { return new SqlString("251" + thePostcode); }
        else if (theCountry == "MOROCCO") { return new SqlString("252" + thePostcode); }
        else if (theCountry == "MOZAMBIQUE") { return new SqlString("253" + thePostcode); }
        else if (theCountry == "MYANMAR") { return new SqlString("254" + thePostcode); }
        else if (theCountry == "NAMIBIA") { return new SqlString("255" + thePostcode); }
        else if (theCountry == "NAURU") { return new SqlString("256" + thePostcode); }
        else if (theCountry == "NEPAL") { return new SqlString("257" + thePostcode); }
        else if (theCountry == "NETHERLANDS") { return new SqlString("259" + thePostcode); }
        else if (theCountry == "NETHERLANDS ANTILLES") { return new SqlString("258" + thePostcode); }
        else if (theCountry == "NEW CALEDONIA") { return new SqlString("260" + thePostcode); }
        else if (theCountry == "NEW ZEALAND") { return new SqlString("261" + thePostcode); }
        else if (theCountry == "NICARAGUA") { return new SqlString("262" + thePostcode); }
        else if (theCountry == "NIGER") { return new SqlString("263" + thePostcode); }
        else if (theCountry == "NIGERIA") { return new SqlString("264" + thePostcode); }
        else if (theCountry == "NIUE") { return new SqlString("265" + thePostcode); }
        else if (theCountry == "NORFOLK ISLAND") { return new SqlString("356" + thePostcode); }
        else if (theCountry == "NORTH KOREA") { return new SqlString("266" + thePostcode); }
        else if (theCountry == "NORTHERN MARIANA ISLANDS") { return new SqlString("267" + thePostcode); }
        else if (theCountry == "NORWAY") { return new SqlString("268" + thePostcode); }
        else if (theCountry == "OMAN") { return new SqlString("269" + thePostcode); }
        else if (theCountry == "PAKISTAN") { return new SqlString("270" + thePostcode); }
        else if (theCountry == "PALAU") { return new SqlString("271" + thePostcode); }
        else if (theCountry == "PALESTINE") { return new SqlString("272" + thePostcode); }
        else if (theCountry == "PANAMA") { return new SqlString("273" + thePostcode); }
        else if (theCountry == "PAPUA NEW GUINEA") { return new SqlString("274" + thePostcode); }
        else if (theCountry == "PARAGUAY") { return new SqlString("275" + thePostcode); }
        else if (theCountry == "PERU") { return new SqlString("276" + thePostcode); }
        else if (theCountry == "PHILIPPINES") { return new SqlString("277" + thePostcode); }
        else if (theCountry == "PITCAIRN ISLAND") { return new SqlString("278" + thePostcode); }
        else if (theCountry == "POLAND") { return new SqlString("279" + thePostcode); }
        else if (theCountry == "PORTUGAL") { return new SqlString("280" + thePostcode); }
        else if (theCountry == "PUERTO RICO") { return new SqlString("281" + thePostcode); }
        else if (theCountry == "QATAR") { return new SqlString("282" + thePostcode); }
        else if (theCountry == "REPUBLIC OF CONGO") { return new SqlString("361" + thePostcode); }
        else if (theCountry == "REPUBLIC OF GEORGIA") { return new SqlString("188" + thePostcode); }
        else if (theCountry == "REUNION") { return new SqlString("286" + thePostcode); }
        else if (theCountry == "ROMANIA") { return new SqlString("287" + thePostcode); }
        else if (theCountry == "RUSSIA") { return new SqlString("288" + thePostcode); }
        else if (theCountry == "RWANDA") { return new SqlString("289" + thePostcode); }
        else if (theCountry == "SAMOA") { return new SqlString("290" + thePostcode); }
        else if (theCountry == "SAN MARINO") { return new SqlString("291" + thePostcode); }
        else if (theCountry == "SANDWICH ISLANDS") { return new SqlString("362" + thePostcode); }
        else if (theCountry == "SAO TOME & PRINCIPE") { return new SqlString("292" + thePostcode); }
        else if (theCountry == "SAUDI ARABIA") { return new SqlString("293" + thePostcode); }
        else if (theCountry == "SENEGAL") { return new SqlString("294" + thePostcode); }
        else if (theCountry == "SERBIA") { return new SqlString("295" + thePostcode); }
        else if (theCountry == "SEYCHELLES") { return new SqlString("296" + thePostcode); }
        else if (theCountry == "SIERRA LEONE") { return new SqlString("298" + thePostcode); }
        else if (theCountry == "SINGAPORE") { return new SqlString("284" + thePostcode); }
        else if (theCountry == "SLOVAKIA") { return new SqlString("299" + thePostcode); }
        else if (theCountry == "SLOVENIA") { return new SqlString("300" + thePostcode); }
        else if (theCountry == "SOLOMON ISLANDS") { return new SqlString("302" + thePostcode); }
        else if (theCountry == "SOMALIA") { return new SqlString("303" + thePostcode); }
        else if (theCountry == "SOUTH AFRICA") { return new SqlString("304" + thePostcode); }
        else if (theCountry == "SOUTH KOREA") { return new SqlString("359" + thePostcode); }
        else if (theCountry == "SPAIN") { return new SqlString("305" + thePostcode); }
        else if (theCountry == "SRI LANKA") { return new SqlString("306" + thePostcode); }
        else if (theCountry == "ST HELENA") { return new SqlString("307" + thePostcode); }
        else if (theCountry == "ST KITTS & NEVIS") { return new SqlString("308" + thePostcode); }
        else if (theCountry == "ST LUCIA") { return new SqlString("309" + thePostcode); }
        else if (theCountry == "ST PIERRE & MIQUELON") { return new SqlString("311" + thePostcode); }
        else if (theCountry == "ST VINCENT & THE GRENADINES") { return new SqlString("312" + thePostcode); }
        else if (theCountry == "SUDAN") { return new SqlString("313" + thePostcode); }
        else if (theCountry == "SURINAME") { return new SqlString("314" + thePostcode); }
        else if (theCountry == "SWAZILAND") { return new SqlString("315" + thePostcode); }
        else if (theCountry == "SWEDEN") { return new SqlString("316" + thePostcode); }
        else if (theCountry == "SWITZERLAND") { return new SqlString("317" + thePostcode); }
        else if (theCountry == "SYRIA") { return new SqlString("318" + thePostcode); }
        else if (theCountry == "TAIWAN") { return new SqlString("320" + thePostcode); }
        else if (theCountry == "TAJIKISTAN") { return new SqlString("321" + thePostcode); }
        else if (theCountry == "TANZANIA") { return new SqlString("322" + thePostcode); }
        else if (theCountry == "THAILAND") { return new SqlString("324" + thePostcode); }
        else if (theCountry == "TIMOR-LESTE") { return new SqlString("167" + thePostcode); }
        else if (theCountry == "TOGO") { return new SqlString("325" + thePostcode); }
        else if (theCountry == "TOKELAU") { return new SqlString("326" + thePostcode); }
        else if (theCountry == "TONGA") { return new SqlString("327" + thePostcode); }
        else if (theCountry == "TRINIDAD & TOBAGO") { return new SqlString("328" + thePostcode); }
        else if (theCountry == "TRISTAN DA CUNHA") { return new SqlString("355" + thePostcode); }
        else if (theCountry == "TUNISIA") { return new SqlString("330" + thePostcode); }
        else if (theCountry == "TURKEY") { return new SqlString("331" + thePostcode); }
        else if (theCountry == "TURKMENISTAN") { return new SqlString("332" + thePostcode); }
        else if (theCountry == "TURKS AND CAICOS") { return new SqlString("333" + thePostcode); }
        else if (theCountry == "TUVALU") { return new SqlString("334" + thePostcode); }
        else if (theCountry == "UGANDA") { return new SqlString("335" + thePostcode); }
        else if (theCountry == "UKRAINE") { return new SqlString("336" + thePostcode); }
        else if (theCountry == "UNITED ARAB EMIRATES") { return new SqlString("337" + thePostcode); }
        else if (theCountry == "UNITED KINGDOM") { return new SqlString("338" + thePostcode); }
        else if (theCountry == "URUGUAY") { return new SqlString("340" + thePostcode); }
        else if (theCountry == "UZBEKISTAN") { return new SqlString("342" + thePostcode); }
        else if (theCountry == "VANUATU") { return new SqlString("343" + thePostcode); }
        else if (theCountry == "VATICAN CITY") { return new SqlString("344" + thePostcode); }
        else if (theCountry == "VENEZUELA") { return new SqlString("345" + thePostcode); }
        else if (theCountry == "VIETNAM") { return new SqlString("346" + thePostcode); }
        else if (theCountry == "UNITED STATES") { return new SqlString("339" + thePostcode); }
        else if (theCountry == "US VIRGIN ISLANDS") { return new SqlString("341" + thePostcode); }
        else if (theCountry == "WALLIS & FUTUNA") { return new SqlString("347" + thePostcode); }
        else if (theCountry == "YEMEN") { return new SqlString("349" + thePostcode); }
        else if (theCountry == "ZAMBIA") { return new SqlString("352" + thePostcode); }
        else if (theCountry == "ZIMBABWE") { return new SqlString("353" + thePostcode); }

        else return new SqlString ("");
    }
}

理想情况下,您应该将该数据保存在一个单独的表中并加入到该表中,但是如果您想按照现在的方式进行操作,那么使用静态只读
字典将大大缩短响应时间。

此函数花了42秒来浏览SQL中包含992833条记录的列表。不错,但希望速度快一点!你为什么要用函数来做这个?与拥有一个包含国家和国家代码的表相比,您只需加入一个国家代码?索引需要多少钱?@Daniel,使用一个索引国家表和一个内联表值函数需要20秒,所以我想我会忘记使用CLR进行此操作的愚蠢想法。:)另外,在维护列表时,您可能会很高兴它没有全部编译到函数中。谢谢Nathan,您和其他人一样是对的。我放弃了CLR,转而使用SQL内联表值函数。谢谢