sql-server-2012

Retrieving the last inserted ID using scope_Identity()

守給你的承諾、 提交于 2021-01-28 05:31:40
问题 I am trying to get the last ID inserted using the SCOPE_IDENTITY but I got an error. I am using SQL server 2012 with Visual Studio Express 2013 Here is my code protected void PerformInscription(string sEmail, string sPassword,string sName) { bool buserIdAuthenticated = false; string salt = null; string passwordHash = pwdManager.GeneratePasswordHash(txtPassword.Text, out salt); SqlConnection sqlConnection; sqlConnection = new SqlConnection(); try { sqlConnection.ConnectionString =

SQL | Return MIN values | multiple rows dynamically

筅森魡賤 提交于 2021-01-28 03:20:22
问题 I need to create a query that returns only the rows which have the minimum values in a specific column. I have this results (example): Name | Description | Order ----------------------------- Name1 | A | 1 Name1 | B | 2 Name1 | C | 3 Name2 | A | 1 Name2 | B | 2 Name2 | C | 3 I want to get this results: Name | Description | Order ----------------------------- Name1 | A | 1 Name2 | A | 1 Basically, I need to select only the rows which have the minimum value in the column order . I'm stuck with

SQL Server group by absorb null and empty values

泄露秘密 提交于 2021-01-28 02:54:35
问题 I have this data: Id Name amount Comments ------------------------------- 1 n1 421762 Hello 2 n2 421 Bye 3 n2 262 null 4 n2 5127 '' Each name may or may not have extra rows with null or empty comments. How can I group by name and sum(amount) such that it ignores/absorbs the null or empty comments in the grouping and shows me only 2 groups. Output I want: Id Name sum(amount) Comments ------------------------------------ 1 n1 421762 Hello 2 n2 5180 Bye I can't figure this out. I hoped that

How to EXEC a stored procedure from SSIS to get its output to text file

北城以北 提交于 2021-01-28 00:41:01
问题 I wrote a stored procedure that reads records into a temp table and then creates a pivoted output from the data in the temp table. Running the stored procedure from SSMS works just fine. What I am facing problems is now that I am trying to create the SSIS package that will execute the sproc, and write its output to a tab delimited text file. I am using Visual Studio 2015. My first problem is that when I try to configure the OLE DB Source within the Data Flow Task, when adding the SQL Command

SQL Server group by absorb null and empty values

核能气质少年 提交于 2021-01-28 00:02:27
问题 I have this data: Id Name amount Comments ------------------------------- 1 n1 421762 Hello 2 n2 421 Bye 3 n2 262 null 4 n2 5127 '' Each name may or may not have extra rows with null or empty comments. How can I group by name and sum(amount) such that it ignores/absorbs the null or empty comments in the grouping and shows me only 2 groups. Output I want: Id Name sum(amount) Comments ------------------------------------ 1 n1 421762 Hello 2 n2 5180 Bye I can't figure this out. I hoped that

Convert Image DataType To String in SQL Server

谁说胖子不能爱 提交于 2021-01-27 12:36:08
问题 I have a Column having Image datatype in MS-SQL SERVER 2012. I want to get it as string in SQL Query.. I have Tried This: SELECT 'empphoto : '+ ISNULL(CONVERT(VARCHAR(MAX), CONVERT(VARBINARY(MAX), empphoto)),'') from emppersonal where --some condition --empphoto Columns is of Image datatype output looks corrupted (Just 4 characters). OutPut looks like: empphoto : ÿØÿà How can be an Image datatype be converted to string in MS-SQL Server? 回答1: You can extract the image value as BASE64 by

Generate list of new unique random numbers in T-SQL

倖福魔咒の 提交于 2021-01-27 07:38:23
问题 I need a stored procedure to generate @n records, each with a unique random 8 digit number. This number must not be incremental and must not exist already in the table. CREATE TABLE Codes ( ID UNIQUEIDENTIFIER PRIMARY KEY, Code INT, CONSTRAINT UQ_Code UNIQUE(Code) ); I can generate random numbers: DECLARE @min int = 0, @max int = 99999999, @n INT = 100; SELECT TOP (@n) FLOOR(CAST(CRYPT_GEN_RANDOM(4) AS BIGINT) / 4294967296 * ((@max - @min) + 1)) + @min FROM sys.all_objects s1 CROSS JOIN sys

How can I set identity_insert on a tablename passed as a variable

别等时光非礼了梦想. 提交于 2021-01-27 07:22:45
问题 I have the Database name in a @strDBName . I build the SET IDENTITY_INSERT and execute it. There's no error, but a subsequent insert fails. The code below illustrates the problem. Declare @Query Varchar(MAX) SET @Query = 'SET IDENTITY_INSERT '+ @strDBName +'..TableName ON' EXEC(@Query) INSERT INTO [TableName] ... (MAX) Value from another table and other applicable record. SET @Query = 'SET IDENTITY_INSERT '+ @strDBName +'..TableName OFF' EXEC(@Query) 回答1: Just to backup Brad's answer given in

SQL Server Agent - SSIS Package - Error 0x80131904 - Timeout expired

人盡茶涼 提交于 2021-01-21 08:32:31
问题 There's been a string of random occurrences of the following error in the SQL Server Agent scheduled jobs lately that I have been unable to find a solution to. The error occurs infrequently, but usually once a week for a daily scheduled job, but in any number of different jobs and not always the same one. Each job shares the fact that it executes an SSIS package from the same server that is running the job. It also always runs for almost exactly 30 seconds elapsed time, which I guess is the

Integration SP Query execution shows unknown error

旧城冷巷雨未停 提交于 2021-01-20 10:57:21
问题 Executed SQL query: --Scheduler SP EXEC [SQLOAM].[OAM_SP_MPS_Print_Traces_Insert] @intCompanyCode = 1, @chrDomain = 'ECC-WEB', @dtInsertedFrom ='01-Apr-2015', @dtInsertedTo ='11-Aug-2015', @intUID = 169558, @intCommunityCode = 1 This error appears on exec that query: Msg 8106, Level 16, State 1, Procedure OAM_SP_MPS_Print_Traces_Insert, Line 105 Table 'sqlmas.GEN_U_Paper_Size' does not have the identity property. Cannot perform SET operation. Code: UPDATE #Temp_OAM_MPS_Print_Traces SET Temp