300x250 AD TOP

Powered by Blogger.

Home Page

Contributors

Blogger news

Tagged under:

Generating Identity Column in SELECT


Generating Identity Column in SELECT statements at runtime


There are situations where we need to generate an identity column [run time] seeding 1 when inserting records into staging or another tables. For example, you have a table where you have a primary key and identity has not been set on this primary key column.

If you want to insert records into this table on daily basis you need to generate the value for identity column when you are trying to insert records using select statements.

There are two easy ways to generate an primary identity column under select statements:

1. If you are inserting values using INTO statement you can use IDENTITY(INT,1,1) function in your select statement to generate unique values as shown below:

SELECT
 IDENTITY(INT, 1,1) AS EmployeeID,
 VALUE INTO #Ranks
FROM YourTable
WHERE 1=0

INSERT INTO #Ranks SELECT SomeColumn FROM YourTable ORDER BY SomeColumn

SELECT * FROM #Ranks Order By Ranks

The EmployeeID will be generated with unique identity number.

2. Another way of doing this is using; ROW_NUMBER() function,

SELECT ROW_NUMBER() OVER (ORDER BY colA) AS Row, cola
                FROM table1
This will also generates column with unique value seeded by 1.

Hope this helps!
Tagged under:

SQL Server Error:53 the network path was not found

ADDITIONAL INFORMATION:

Error:
Could not open connection to SQL Server Error:53 the network path was not found


A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 53)

When you are trying to connect to SQL Server from SSMS you may face this error because of following reasons.

There are many generic reasons for this, few are below:
  1.  Incorrect connection string.
  2.  Named Pipes was not enabled on the SQL instance.
  3.  Remote connection was not enabled.
  4.  Server not started, or point to not a real server in your connection string.
  5.  Other reasons such as incorrect security context.
  6. Check the firewall.

The main reason would be you are not providing the right instance name in SSMS when you are connection to Database Engine.

You can find the instance name by the following methods:

1. Click Start--> Run --> Type CMD
type:  hostname Press enter
Result would be you system name: HOME
2. Then Open the SQL Server Configuration Manager
Click Start --> Programs --> SQL Server 2012 --> Configuration tools --> SQL Server Configuration Manager



check for the SQL Server instance name inside the parenthesis in the configuration manager (Ex: SQLSERVER2012)
Now the instance in SSMS should be:
HOME\SQLSERVER2012

This will connect you to your database engine.

Hope this helps!
Tagged under:

Content within this application coming from the website listed is being blocked by internet explorer




This error seems like problem with Internet Explorer, but you will be getting this error due to the setting in the Outlook in most of the cases.



To over come this error, you need to change the People Pane setting in the outlook.
Follow the below steps:
1. Open outlook.
2. Goto to the View tab
3. Click on the People Pane and make it "Off".

This works for me. :)









Tagged under:

Identify Missing Sequence Numbers SQL Server



There are many scenarios where we may in need to identify the missing numbers in the provided range.

Here is the quick query which help in identifying using CTE [Common Table Expression]:

DECLARE @Test TABLE
(
Num INT
)

INSERT INTO @Test VALUES (1)
INSERT INTO @Test VALUES (2)
INSERT INTO @Test VALUES (4)
INSERT INTO @Test VALUES (5)
INSERT INTO @Test VALUES (8)
INSERT INTO @Test VALUES (9)
INSERT INTO @Test VALUES (10)

--Get the Missing Numbers from the sequence
;WITH Missing (minid, maxid)
AS
(
SELECT 0 AS minid, 10
UNION ALL
SELECT minid + 1, maxid FROM Missing
WHERE minid < maxid
)
SELECT minid
FROM Missing
LEFT OUTER JOIN @Test tt on tt.Num = Missing.minid
WHERE tt.Num is NULL
OPTION (MAXRECURSION 0);

Result:
minid
0
3
6
7
Tagged under:

You receive an “Explicit value must be specified for identity column” error message when you use the replication feature to run an INSERT statement on a table in SQL Server


If you are facing this error "You receive an “Explicit value must be specified for identity column” error message when you use the replication feature to run an INSERT statement on a table in SQL Server" when you are running transaction replications, here is the reason behind this.

You are trying to insert a row in the secondary server where the table property "NOT FOR REPLICATION" option is enabled.
To work around this issue, disable the NOT FOR REPLICATION option for the IDENTITY column.
Here is how we can check the value:

The "Not For Replication" setting for identity columns allows replication to pass the ID value from the publisher to the subscriber without the subscriber creating a new ID.  Since identity columns will always generate a new number when records are inserted, this setting allows you to ensure the values on both your publisher and subscribers stay in synch.
This option can be set when designing or creating a new table as shown below in the highlighted section.
Right click on the table in the secondary server, click on Design in context menu,
check in the properties whether the "NOT FOR REPLICATION" property is set to "yes".



If this value is set to "yes" change it to "no" using the below script

declare @objid int
select @objid = object_id('Adventureworks')
exec sys.sp_identitycolumnforreplication @objid, 0

The above query will set the "NOT FOR REPLICATION" property to "No". Once you set the value you need to perform the reinitialize the replication to get all the old values.

Ref. MSDN Article: http://support2.microsoft.com/kb/908711
http://www.mssqltips.com/sqlservertip/1274/change-not-for-replication-value-for-sql-server-identity-columns/
Tagged under:

Could not load file or assembly '' or one of its dependencies. An attempt was made to load the program with an incorrect format.


  

Could not load file or assembly '' or one of its dependencies. An attempt was made to load the program with an incorrect format.

If you have any VC++ CLI components in your azure web role and you are facing the above error, then this post might be helpful for you to resolve your issue.

One of the most common problems reported regarding Windows Azure Web Sites is “Assembly Loading” issue. 
Two main things you need to verify,
  • This error might be causing because Azure runtime is unable to find the dll/assembly that it needs to run the web role.
  • Secondly, this error might occur if you are trying to run any of the 32-bit components in windows azure.
 When you create a web role and try to host on Windows Azure you may seen this error because the Azure runtime was unable to load the necessary assemblies to make your web role run successfully.
Now, the question is how to identify which assembly is not loading as expected:

First check:
There is a program called Dependency Walker which allows you to see the dependencies of a given PE file (dll, exe, ocx...).
This error is really annoying, and very difficult to debug. You have to make sure that your dll is present as well as ANY dependency this dll has.
If you see that any dll/assembly is missing ensure you package that dll into your azure package.

Second Check:
Make sure you run your web role and all the other project (mainly VC++ CLI Components) inside the webrole in "Release" Mode. The reason, few VC++ debug dll cannot be available in Azure Emulator and might cause this issue. :) (This was my scenario, I struggled one week to identify this).



 Third Check:
Few additional settings you need to verify, Check whether you AppPool is enabled for 32-bit applications.
Go to Respective ==> Site Application Pool ,
Click on ==> Advance Settings
Change value of ==> "Enable 32-Bit Applications" to True.

 Fourth Check:
The app would run fine on the developer machines and select others machines but not on recently installed machines. In this case, you need to verify whether the respective Visual C++  runtime installed or not. Adding the respective Visual C++ Runtime redistributable to the app installer will fix the issue.

And finally,
Ensure you have packages all the respective dll into your Azure packaging, to do that
1. If you want an your file to be copied, first you can add it to the project.
2. After adding right click on the file --> properties
3. Set the Build Action from "None" to "Content"
4. or The other option you have is to keep the Build Action as "None" but set "Copy to Output Directory" to "Copy if Newer" (or "Copy Always"). 

 This will include your file in Azure packaging.

Hope this resolves your issue, Happy coding..