How do I find a string after a query?

I have a request and I need to find the line number in which the request returns the response. I don't have a counter field. How to do it?

Thanks in advance.

0


a source to share


4 answers


SELECT 
  ROW_NUMBER() OVER (<your_id_field_here> ORDER BY <field_here>) as RowNum,
  <the_rest_of_your_fields_here>
FROM
  <my_table>

      



+2


a source


If you have a primary key, you can use this method on SQL Server 2005 and up:

SELECT 
  ROW_NUMBER() OVER (ORDER BY PrimaryKeyField) as RowNumber,
  Field1,
  Field2
FROM
  YourSourceTable

      

If you don't have a primary key, you may need to duplicate the table into the mermory table (or a temporary table if it is very large) using the following method:



DECLARE @NewTable table (
    RowNumber BIGINT IDENTITY(1,1) PRIMARY KEY,
    Field1 varchar(50),
    Field2 varchar(50),
)

INSERT INTO @NewTable
    (Field1, Field2)
SELECT
    Field1,
    Field2
FROM
    YourSourceTable

SELECT
    RowNumber,
    Field1,
    Field2
FROM
    @NewTable

      

The nice part of this is that you will be able to find duplicate rows if your original table does not have a primary key.

Also, at this point, I would suggest adding a primary key to each table if you don't already have one.

0


a source


create table t1 (N1 int, N2 int)

insert in t1 select 200 300

insert in t1 select 200 300

insert in t1 select 300 400

insert in t1 select 400 400 ..... ......

select row_number () over (order from [N1]) RowNumber, * from t1

0


a source


I have a request and I need to find the line number that returns the request response. I don't have a counter box. how do i do it?

If I understand your question correctly, you have some kind of query that returns a string or multiple lines (answer, answers) and you want the number of lines to be associated with the answer

As you said, you don't have a counter field, so you need the following:

  • select order criteria
  • add counter
  • run request

It will really help if you provide more details on your questions. If you confirm that I understood your question correctly, I will add a sample code

0


a source







All Articles