Tag: sql-server

Why return -1 is not working as expected

I am new to SQL Server. Please help me with this issue RAISERROR(‘There is a problem in the code’,16,1) RETURN -1 This is working perfectly fine. So I put this in a common procedure like this: CREATE procedure [dbo].[Raise_Error] (@ErrorMsg varchar(6000) , @ErrorSeverity integer = 16, @ErrorState integer = 1) AS BEGIN RAISERROR (@ErrorMsg, @ErrorSeverity, […]

Use session variable or copy PHP code from second page to third page of a website

I have three pages in the website. The first is login page,second is profile page and third is main page. <?php session_start(); $servername=”blah blah blah”; $connectioninfo=array(‘Database’=>’mbr’); $conn=sqlsrv_connect($servername,$connectioninfo); if($conn) { echo ‘connection established’; } else { echo ‘connection failure’; die(print_r(sqlsrv_errors(),TRUE)); } $q1=”SELECT * FROM EmployeeTable WHERE EmployeeID = ‘” . $_SESSION[‘id’] . “‘ “; $stmt=sqlsrv_query($conn,$q1); if($stmt==false) […]

Stored Procedure to create table using Data table and table name as parameters

I’m trying to create a stored procedure that will accept DataTable and Table name as parameters and creates a table. This is the procedure: ALTER PROCEDURE [dbo].[SPROC_IMPORT_UPLOADED_EXCEL_DATA] @DataTbl emTable READONLY , @TableName [varchar](50) AS BEGIN DECLARE @SQL nvarchar(MAX) ——————————————————————————- –Check 1: Checking if the table is already present ——————————————————————————- IF OBJECT_ID (@TableName,’U’) IS NOT NULL […]

How can i convert time to minutes using Sql queries e.g “02:30:05' to 150.5 minutes

I am working on a billing system and its based on time-in and time-out. Rate is R5 per 30 minutes charged to users for loaning an item. I managed to get the Time difference and got time as “02:30:05” now how can i convert this time to minutes and have “02:30:00” = “150 min” so […]

SQL Server 2014: SELECT only those rows that match all rows in another table

I am a beginner in SQL Server. I am trying to solve this problem: Select all (distinct) “item_id” from “ItemTag” table whose corresponding “tag_id” values match at least “all” values in the “UserTagList” table. I tried a join below but instead of the result I got the query should return the item_id 5 since it […]

SQL Server running total based on change of state of a column

I am trying to calculate a running total of values based upon a flip/flop column with the Start and End date/time. Here is a sample set of data: Sample Input Data: StartDate EndDate Count FlipFlop 2016-08-28 16:34:15.380 2016-08-28 16:34:15.990 1 0 2016-08-28 16:34:15.990 2016-08-28 16:34:17.973 0 0 2016-08-28 16:34:17.973 2016-08-28 16:35:00.463 5 0 2016-08-28 16:35:00.463 […]

select combined with an update, race condition

I would like to do something like this begin trans declare @maxpriceprice int; Select @maxpriceprice MAX(price) from products where typeId = 5 and later on Insert into products (price) values(@maxpriceprice + RAND() * 10) commit tran It is rather strange but it is strange application. between the select and insert i cannot afford to havve […]

Database call stuck at java.net.SocketInputStream.socketRead0(Native Method)

Calls made from Java to SQL server using jtds driver is stuck. We have checked there is no locking or any long running queries on SQL server. Following is the stack trace from java thread dump. Any one has any idea? “core-CommandInvoker-thread-7133” prio=5 tid=7362 @@@@ RUNNABLE at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(SocketInputStream.java:152) at java.net.SocketInputStream.read(SocketInputStream.java:122) at java.io.DataInputStream.readFully(DataInputStream.java:195) […]

SQL table holding incremental counter

I need to update a table with a counter – its quite a simple setup. The table will have no more than 10 rows. At present there is a PrimaryIndex on a 3-char string column (SiteCode) with a second column (NumVotes) holding a long number. The idea is to increment NumVotes with thread safety and […]

Can't login to SQL Server using SQL Instance Name

I am new in SQL. I have installed SQL Sever 2016 & SSMS2016. My SQL server instance name is MSSQLSERVER (found it in Services list). When I am trying this servername RITWICK\MSSQLSERVER , it’s showing me incorrect. But when I use just RITWICK, no problem.

MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.