Showing posts with label sql 2012. Show all posts
Showing posts with label sql 2012. Show all posts

Monday, November 24, 2014

A correlation name must be specified for the bulk rowset in the from clause.



A correlation name must be specified for the bulk rowset in the from clause.

While inserting image files into a SQL Server database table using the OPENROWSET function, I got the following error message from the SQL engine.
Msg 491, Level 16, State 1, Line 5
A correlation name must be specified for the bulk rowset in the from clause.
I soon realized that the error message is asking for a alias name for the OPENROWSET select statement.



A correlation name must be specified for the bulk rowset in the from clause.

While inserting image files into a SQL Server database table using the OPENROWSET function, I got the following error message from the SQL engine.
Msg 491, Level 16, State 1, Line 5
A correlation name must be specified for the bulk rowset in the from clause.
I soon realized that the error message is asking for a alias name for the OPENROWSET select statement.



Here is the t-sql script that is causing the error message :
INSERT INTO Files(fname, [file])
SELECT 'T-SQL-Enhancements-in-SQL-Server-2008', * FROM OPENROWSET(
  BULK N'C:\T-SQL-Enhancements-in-SQL-Server-2008.jpg', SINGLE_BLOB
)

And the below sql script displays how the correct statement should be built.
Take your attention on the "rs" alias name at the end of the script.
INSERT INTO Files(fname, [file])
SELECT 'T-SQL-Enhancements-in-SQL-Server-2012', * FROM OPENROWSET(
  BULK N'C:\T-SQL-Enhancements-in-SQL-Server-2012.jpg', SINGLE_BLOB
) rs

 examples
--INSERT
INSERT INTO  [dbo].[TsOnProfile]([Photo])SELECT * FROM OPENROWSET( BULK N'C:\1.png', SINGLE_BLOB) rs 

--update
update [dbo].[TsOnProfile] set[Photo]= (SELECT * FROM OPENROWSET(
  BULK N'C:\1.png', SINGLE_BLOB
) rs)

ref:
http://www.kodyaz.com/articles/correlation-name-for-bulk-rowset-in-from-clause.aspx

Friday, July 4, 2014

SQL Server: Limit (MySQL) equal Function in SQL Server

SQL Server: Limit (MySQL) equal Function in SQL Server

In MySQL, Limit is a powerful function used with SELECT query to return rows BUT within given range. We commonly need it when returning only required rows to application according to paging range. For example to return rows from 11 to 15 we will use following query in MySQL
SELECT * FROM MyTable LIMIT 11, 5

In SQL Server, same functionality can be achieved with three different ways.
1.       With Derived Table (Most inefficient way, but applicable to all versions of SQL Server)

SELECT  *
      FROM    ( SELECT    *, ROW_NUMBER() OVER ( ORDER BY ColumnName ) AS RowNum
          FROM      MyTable ) DerivedTable
       WHERE   RowNum >= 11
        AND RowNum <= 11 + ( 5 - 1 )

2.       With CTE Common Table Expression (Applicable to SQL Server 2005/2008/2012)
   ;
     WITH   CTE
          AS ( SELECT   *, ROW_NUMBER() OVER ( ORDER BY ColumnName ) AS RowNum
               FROM     MyTable)
    SELECT  *
    FROM    CTE
    WHERE   RowNum >= 11
            AND RowNum <= 11 + ( 5 - 1 )

3.       With OFFSET_ROW FETCH (Applicable to SQL Server 2012 Only)
        Here “OFFSET means, how many rows to skip, “ROWS FETCH NEXT” means, how many rows to skip

 SELECT * FROM MyTable  
        OFFSET 10 
        ROWS FETCH NEXT 5 ROWS ONLY;


ref:
http://www.connectsql.com/2012/07/sql-server-limit-mysql-equal-function.html

MS in Computer Science with paid training in USA company